Solved

SQL query with User input

Posted on 2011-09-22
7
210 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Creating and Managing Databases with phpMyAdmin in cPanel.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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