denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 499 forks source link

HOW to create sqlserver schema #738

Open zhanghaiyang9999 opened 2 years ago

zhanghaiyang9999 commented 2 years ago

I use the following codes to create schema, but always failed: strsql := USE [test001]; GO CREATE SCHEMA [test3] GO ,err := conn.Exec(str_sql)

always return the error: CREATE SCHEMA' must be the first statement in a query batch.

Can anyone help this issue, thanks!

Describe the bug A clear and concise description of what the bug is.

If you are seeing an exception, include the full exceptions details (message and stack trace).

Exception message:
Stack trace:

To Reproduce Include a complete code listing that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

Expected behavior A clear and concise description of what you expected to happen.

Further technical details

SQL Server version: (e.g. SQL Server 2017) Operating system: (e.g. Windows 2019, Ubuntu 18.04, macOS 10.13, Docker container) Table schema

Additional context Add any other context about the problem here.

bokwoon95 commented 2 years ago
  1. Remove the GO statement. That is not valid SQL, it only works in SSMS.
  2. You cannot use CREATE SCHEMA together with other SQL statements (https://stackoverflow.com/q/5748056). Wrap it in EXEC().
conn.Exec("USE [test001]; EXEC('CREATE SCHEMA [test3]');")
kardianos commented 1 year ago

There is a sub package to break up go statement.

On Sat, May 21, 2022, 00:52 bokwoon95 @.***> wrote:

  1. Remove the GO statement. That is not valid SQL, it only works in SSMS.
  2. You cannot use CREATE SCHEMA together with other SQL statements ( https://stackoverflow.com/q/5748056). Wrap it in EXEC().

conn.Exec("USE [test001]; EXEC('CREATE SCHEMA [test3]');")

— Reply to this email directly, view it on GitHub https://github.com/denisenkom/go-mssqldb/issues/738#issuecomment-1133541226, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFYLMO4GGUK4A7HQ5XA37DVLB22VANCNFSM5UU3W4WA . You are receiving this because you are subscribed to this thread.Message ID: @.***>