Solved

SQL query with User input

Posted on 2011-09-22
7
194 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 142

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

11 Experts available now in Live!

Get 1:1 Help Now