Solved

Variable in query

Posted on 2013-11-30
6
172 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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

17 Experts available now in Live!

Get 1:1 Help Now