go-mssqldb icon indicating copy to clipboard operation
go-mssqldb copied to clipboard

HOW to create sqlserver schema

Open zhanghaiyang9999 opened this issue 2 years ago • 2 comments

I use the following codes to create schema, but always failed: str_sql :=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.

zhanghaiyang9999 avatar Apr 29 '22 06:04 zhanghaiyang9999

  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]');")

bokwoon95 avatar May 21 '22 05:05 bokwoon95

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: @.***>

kardianos avatar Oct 11 '22 08:10 kardianos