?
Solved

Dynamic Sql

Posted on 2010-01-06
5
Medium Priority
?
146 Views
Last Modified: 2012-05-08
i am trying to write sql query for SSRS report that should have 2 pramaeter. How can i write it in such a way that i can pass value to the parameters and  get results and it should also give all the results without passing any parameter values how can i do that
0
Comment
Question by:Svlss
  • 3
5 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 26195051
basicly like this

declare @param1      int;
declare @param2      varchar;

SELECT *
FROM yourTable
WHERE col1 = @param1
  AND col2 = @param2
0
 

Author Comment

by:Svlss
ID: 26195136
but it if i dont enter any value and run report it dosent run
0
 
LVL 26

Accepted Solution

by:
tigin44 earned 2000 total points
ID: 26195170
declare @param1      int;
declare @param2      varchar;

SELECT *
FROM yourTable
WHERE col1 = ISNULL(@param1, col1)
  AND col2 = ISNULL(@param2, col2)
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 26195172
try this:

SELECT *
FROM yourTable
WHERE (col1 = @param1 or @param1 is null)
  AND (col2 = @param2 or @param2 is null)
0
 
LVL 26

Expert Comment

by:tigin44
ID: 26195186
if you can provide your query and define your requirements we may propose other solutions..
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

850 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