6

Background:

I'm building a SQL command to be executed on the server which involves a lot of accessing store procedure and declaring variables which are used as input to other stored procedures. This is all generated on the client side using C# and send as a big package. I have to do it this way since the ping times are very long since the client will be located very remotely with bad connections and I don't want to send many packages and starting building up a backlog due long response times since.

Currently no more than three client can work in parallel or they start to build up a backlog since the transfer speed of one transaction with about 200 store procedures takes about 35 seconds.

Question:

How can I declare a variable which I don't now if it already exists? I could use a dictionary on the C# side but that sounds like the wrong way to do it to me. I want a declare which accepts re-declaring of that variable if it did exist.

2
  • 2
    Why does it matter what the variables are called when you generate the tsql in code? Just name them @V1, @V2 and so on and you will not have to worry about declaring a variable twice. There is no trick that will allow you to do that within one batch anyway. Commented Apr 1, 2015 at 18:53
  • 1
    Because the last value is the relevant. So then ding it i C# is the only way. Commented Apr 1, 2015 at 19:01

2 Answers 2

8

You cannot redeclare variables, nor can you test to see if they have been declared (at least not that I have ever been able to find). BUT, you shouldn't need to do this anyway. If you know the variable names ahead of time, just declare them all at the beginning of the process. Then, use them throughout the script.

For example:

--------------------------------
-- BEGIN script header
--------------------------------
DECLARE @SomeVariable1 INT,
        @SomeVariable2 VARCHAR(50),
        ....;
        SomeVariableN DATETIME;
--------------------------------
-- END script header
--------------------------------

And then add 1 or more script blocks that use these variables:

---- Script Block 1 ---
EXEC dbo.MyProc @InputParam = 1, @OutputParam = @SomeVariable1 OUTPUT;
-----------------------

---- Script Block 2 ---
EXEC dbo.AnotherProc @InputParam = @SomeVariable1;
-----------------------

---- Script Block 3 ---
SET @SomeVariable2 = NULL; -- reset value if you don't want to carry over prior value
..do something here...
-----------------------
2
  • To add to this answer. "The scope of a variable is the range of Transact-SQL statements that can reference the variable. The scope of a variable lasts from the point it is declared until the end of the batch or stored procedure in which it is declared." See: technet.microsoft.com/en-us/library/…
    – Jeremy
    Commented Apr 1, 2015 at 20:05
  • 1
    Spot on. I will do it this way. Commented Apr 2, 2015 at 8:56
5

From reading your question I think you are building the SQL code in your application and then sending it to the server as one long script with multiple statements.

If this is the case you can separate your statements with GO to create batches. The following is valid SQL:

DECLARE @a INT;
SET @a = 1;
SELECT @a;
GO

DECLARE @a INT;
SET @a = 1;
SELECT @a; 

The problem with this is statements can no longer refer to variables declared in separate batches.

I think the real answer is to use stored procedures that are called by your app. This reduces the amount of data sent across the bad link and it ensures the SQL is valid.

5
  • 11
    GO is understood by the client tools (that parse and split into batches) not SQL Server itself. Commented Apr 1, 2015 at 17:13
  • 2
    To emphasize what @Martin is saying, try putting that code inside a stored procedure, single SQL batch from an application, or dynamic SQL called with a single call to sp_executesql. It only appears to work for you because Management Studio has converted it into two separate batches; the OP does not have that luxury. Commented Apr 2, 2015 at 1:53
  • As commented this would not help me. I have to send it all in one go. Commented Apr 2, 2015 at 8:04
  • 1
    Ah yes forgot this was only for SSMS. Commented Apr 2, 2015 at 8:06
  • On the use store procedure thought. I appreciate the suggestion but it. won't work since I have about 10-14 parameters to each store procedure now and I use about 10 different ones. On top of that the number and type of store procedures varies and needs to be dynamic. I could do do a parsing of a string on the server side but that is bad practice since as much of the work should be done on the client since client processing power is cheap. Commented Apr 2, 2015 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.