I'm trying to write a T-SQL script to create a database and the corresponding tables. I'm having a problem where the USE statement complains that the database that I just "created" doesn't exist. If I run the script within SQL Server Management Studio so that I can make use of the GO statement, I don't get this issue.
Is there a T-SQL equivalent of GO that I can use to make sure the CREATE DATABASE gets executed before the USE?
I've tried BEGIN/COMMIT TRANSACTION and BEGIN/END but they didn't help.
GOis a batch separator in SSMS/sqlcmd/etc; there is no equivalent in T-SQL as it is the application that separates the batches. If you need to "emulate" theGOoperator simply send multiple batches from your application.EXECcomes close (EXEC ('CREATE DATABASE Bar')). Not all statements are allowed in a dynamic batch, however, nor are transactions supported for everything (that includes, in particular, database creation). Real, separate command batches that don't depend on the ambient context aren't possible in pure T-SQL alone, requiring complex workarounds like creating and running throwaway agent jobs.SqlCommandin C#.