• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Variable in query

Declare @i varchar(32) ='test';
select col1, col2 from table1 where col3 = @i;

or

select col1, col2 from table1 where col3 = 'test';

Is there any benefit of using Declare @i with simple exec command?

PERL - JSON - ODBC - SQL Server 2008 R2
0
virgo14
Asked:
virgo14
1 Solution
 
Mohit VijayCommented:
user variable only when it can be changed frequently. Else your second command is fine.

like if you have stored procedure and you are getting input, that you are not aware what it can be, use variable in that case.
0
 
QuinnDexCommented:
the use of variable allows the reuse of the query for different inputs on a simple straight forward query like your example use of a variable is not needed.


the majority of users use The database as a backend to another application ie web site, stock control app, performance data app plus 1000's more different uses. the majority of which will query the database to display results.

using variable in your query when querying from an app is usually best practice as the app could re use the query time and time again.

The most common example would be searching for a record on ID

declare @id int
set @id = id
select " from table where ID = @id

Open in new window


bottom line is the use of variable depends entirely on if its single use/function code or could be applied in different circumstance by changing inputs to suit
0
 
mcmonapCommented:
To continue what QuinnDex says if you are creating code for re-use or within an application you should also seriously consider the use of stored procedures instead of embedding SQL code within your application.  By moving the query into a stored procedure you save potential effort in re-coding/re-compiling your application.

eg when your database schema complexity increases and your query changes to:
select t1.col1, t1.col2 from table1 t1 join table2 t2 on t1.id=t2.id where t2.col3 = 'test';
you wouldn't have to update your code if you were actually calling:
usp_getData 'test'

http://technet.microsoft.com/en-us/library/ms191436(v=sql.105).aspx
http://technet.microsoft.com/en-us/library/ms187926(v=sql.105).aspx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
virgo14Author Commented:
I am sorry, I am trying to see if it benefits, putting direct perl variable in query or declaring sql server variable data type and assign argument value to it and then pass to SQL query

Perl code
my $arg1 = $args->{name};
Declare @i varchar(32) = $arg1;
select col1, col2 from table1 where col3 = @i;

or

select col1, col2 from table1 where col3 = $arg1;
0
 
Jim P.Commented:
I am sorry, I am trying to see if it benefits, putting direct perl variable in query or declaring sql server variable data type and assign argument value to it and then pass to SQL query.


Ask us that question after you meet up with Little Bobby Tables.
0
 
QuinnDexCommented:
Yes is the short answer. if the value of the variable changes in the future and you havnt done that then you will have to rewrite the sql query
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now