0

Is there a difference in performance wise by declaring variables like below..

First Method..

DECLARE  @SQLStringCreateView NVARCHAR(MAX)
            ,@SQLStringDropView NVARCHAR(MAX)
            ,@SQLStringCreateSchema NVARCHAR(MAX)
            ,@TableName NVARCHAR(MAX)
            ,@TableCatelog NVARCHAR(MAX)

Second Method..

   DECLARE  @SQLStringCreateView NVARCHAR(MAX)
    DECLARE @SQLStringDropView NVARCHAR(MAX)
    DECLARE @SQLStringCreateSchema NVARCHAR(MAX)
    DECLARE @TableName NVARCHAR(MAX)
    DECLARE @TableCatelog NVARCHAR(MAX)

Or its just good practice..???

5
  • 2
    I believe it's just down to preference. The first doesn't generally work outside of SQL Server but it's a lot easier to write and read. Commented Jun 11, 2015 at 11:34
  • 3
    The two are equivalent, although you should end the statement with a semicolon (and I have a personal preference for not starting lines with commas). Commented Jun 11, 2015 at 11:34
  • 1
    its only for readability Commented Jun 11, 2015 at 11:35
  • During development, I've occasionally found it useful to have separate DECLARE statements, as in your second example; that way, I can comment out or move any of the statements without affecting any of the others. Commented Jun 11, 2015 at 11:41
  • may be the First method wont work in 2005 and later editions but it has been implemented from 2008 declaring Parameter and value Commented Jun 11, 2015 at 11:45

2 Answers 2

4

No, there is no difference. They are doing the exact same thing, and the performance is the same too. It is your own personal preference which of the two you use, there is no valid technical reason to not pick either of them.

Readability and ease of maintenance could be the only reason I can think of to skip the first option.

(If there is any difference, there is a small performance difference in parsing the SQL, but you can neglect that.)

Sign up to request clarification or add additional context in comments.

Comments

0

Well this is just up to you. There is no real difference between your statements. Just a personal reference.

I personally recommend the method with commas in front of the variable. This makes it a bit easier to just outcommend one line.

But just a small hint. Don't use nvarchar(max) for every variable. This could waste too much. ;-) Just use the limit your really needing.

1 Comment

I wouldn't use commas. Al statements now rely on the first one. If you remove that one, the code is invalid. If they have their own declare, stay stand on their own.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.