Solved

Variable in query

Posted on 2013-11-30
6
174 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
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.

 

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now