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
Solved

SQL query with User input

Posted on 2011-09-22
7
218 Views
Last Modified: 2012-05-12
Hi,
Is there a way to write sql query with user input.
Cheers
0
Comment
Question by:RIAS
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36579740
this question is a bit vague, as the issue is: how is the user finally running the sql?
ms sql management studio?
some home-grown app (which programming language)?
using a .sql file?
somehow else?
0
 
LVL 6

Expert Comment

by:kswathi
ID: 36579743
Can you give more details
0
 

Author Comment

by:RIAS
ID: 36579811
I need to save a sql query in database which first tries to  execute before it saves in database.
There are some values in query which will be user input later.
Example query is :
select * from Personal_Details a
inner join DEATH_BENEFIT b on
a.REFNO=b.REFNO

inner join PENSIONER_DETAIL c on
c.REFNO=a.REFNO

inner join TRANSFER_OUT_HISTORY d on
d.REFNO=a.REFNO

where (a.CURRStatus in ('Active','PreservedPensioner','Pensioner','Widow','Child'))
or (a.CURRStatus = 'No Liability'
and ((a.Dateleft > dateadd(y,17/06/2011,-1)) or  (b.dbdate > dateadd(y,17/06/2011,-1))  
or (c.pdpencomm  > dateadd(y,17/06/2011,-1)) or (d.topaid > dateadd(y,17/06/2011,-1))))

bold are the user input.
For the timebeing just need to replace it with user input.

Cheers
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Expert Comment

by:smu95rp
ID: 36580003
Do you mean you want the person running the script (in Management Studio or wherever) to be prompted for parameters at runtime, or just want to know how to use variables?
0
 

Author Comment

by:RIAS
ID: 36580408
Hi,
Just want to know how to use variables.

Cheers
0
 
LVL 2

Accepted Solution

by:
smu95rp earned 500 total points
ID: 36580496
OK

Declare @RefDate datetime

Set @RefDate = '17/06/2011'

Select *
From Personal_Details a
Inner Join DEATH_BENEFIT b  On a.REFNO=b.REFNO
Inner Join PENSIONER_DETAIL c On c.REFNO=a.REFNO
Inner Join TRANSFER_OUT_HISTORY d On d.REFNO=a.REFNO
Where (a.CURRStatus In ('Active','PreservedPensioner','Pensioner','Widow','Child'))
Or (a.CURRStatus = 'No Liability' And ((a.Dateleft > dateadd(y, @RefDate, -1))
    Or  (b.dbdate > dateadd(y, @RefDate, -1))  
    Or (c.pdpencomm  > dateadd(y, @RefDate, -1)) Or (d.topaid > dateadd(y, @RefDate, -1)))
)
0
 

Author Closing Comment

by:RIAS
ID: 36595904
Cheers mate!!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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