Solved

Variable in query

Posted on 2013-11-30
6
181 Views
Last Modified: 2013-12-02
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
Comment
Question by:virgo14
6 Comments
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 39687827
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
 
LVL 9

Accepted Solution

by:
QuinnDex earned 500 total points
ID: 39687845
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
 
LVL 15

Expert Comment

by:mcmonap
ID: 39688612
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:virgo14
ID: 39688790
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
 
LVL 38

Expert Comment

by:Jim P.
ID: 39689027
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
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39689262
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question