- Any way to execute multiple queries on a single connection?

I know I can use a SQL stored procedure, but...
Is there a way to execute multiple queries on a single connection in my codebehind?

cnn = New SqlConnection(ConnString)

cmd = NewSqlCommand("IF OBJECT_ID('tbl_temp','U') IS NOT NULL" & vbCrLf _
       & "DROP TABLE tbl_temp" & vbCrLf _
       & "GO" & vbCrLf _
       & "SELECT *" & vbCrLf _
       & "INTO tbl_temp" & vbCrLf _
       & "FROM fnc_temp()" & vbCrLf _
       & "GO", cnn)

Thank you.
LVL 13
Who is Participating?
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Well that specific query won't work because GO is only recognised by Management Studio and isn't actually a language keyword.
Paul MacDonaldDirector, Information SystemsCommented:
I can't think of any reason that wouldn't work.  I don't think you don't need all the carriage return/line feeds.
Shaun KlineConnect With a Mentor Lead Software EngineerCommented:
You can use the semi-colon as the statement terminator:

IF OBJECT_ID('tbl_temp','U') IS NOT NULL DROP TABLE tbl_temp;
SELECT * INTO tbl_temp FROM fnc_temp();

It's not pretty but works.

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

RickAuthor Commented:
I tried executing without the line feeds, but it was giving me a " Syntax error near 'GO' ".

So I put a break point right before command execution and copied the command text into SQL Management Studio and ran it.
(Because it didn't have line feeds in code-behind, the command text was a long, single line)
It gave me the same error, " Syntax error near 'GO' ".

When I put line feeds in my code-behind and ran it, I got the error message.
I put a break point before command execution and copied the command text into SQL Management Studio and it ran fine.

Read Carl's message -- the GO statement isn't a real SQL command, just something recognized by SQL Management studio, you cannot use it with the  SQLCommand object.

Just use semicolons between each SQL statement and it should work fine, I use similar multiple-statement commands like this all the time.
cmd = NewSqlCommand("IF OBJECT_ID('tbl_temp','U') IS NOT NULL" & vbCrLf _
       & "DROP TABLE tbl_temp" & vbCrLf _
       & "SELECT *" & vbCrLf _
       & "INTO tbl_temp" & vbCrLf _
       & "FROM fnc_temp()" & vbCrLf _
       & , cnn)

Open in new window

RickAuthor Commented:
This works, but the problem is that one of my statements is creating a view and another one is creating a function. So when I remove the GOs from my command string and end each statement with a semicolon, I get:

'CREATE FUNCTION' must be the first statement in a query batch.
'CREATE VIEW' must be the first statement in a query batch.

mortimer452Connect With a Mentor Commented:
Ahh good point.  There is no batch separator equivalent (that I know of) that you can use in straight T-SQL such as you are describing, you're just going to have to follow the MSSQL rules.  I think you're going to have to just run these as separate commands.

cmd=new SQLCommand("CREATE VIEW bla bla bla", cnn)
cmd = new SQLCommand("CREATE FUNCTION bla bla bla", cnn)

Carl TawnSystems and Integration DeveloperCommented:
You should be able to wrap them all up in a transaction an execute it as a single batch too.
RickAuthor Commented:
That's fine... I just wondered that could be done.

Thanks guys.
RickAuthor Commented:
How Carl?

I don't think it will work to wrap everything up in one transactino, but this *might* work.  You could wrap each statement with a BEGIN TRAN / COMMIT TRAN statements to create a new "batch."  

This is sortof a hack though and creates additional overhead on the server for tracking these transactions when it's not really necessary to do so.  There's really no performance advantage to sending these commands as a single command rather than two separate ones, unless you have a really slow network connection and don't want to wait on the additional network IO for connecting a second time.


RickAuthor Commented:
Very good point... thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.