?
Solved

Variable in query

Posted on 2013-11-30
6
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

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…
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…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

771 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