Solved

SQL query with User input

Posted on 2011-09-22
7
224 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
Technology Partners: 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!

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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