Stored procedure query
-
Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,
Agh
-
Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,
Agh
here is the way how to build dynamic query in T-SQL
declare @query as nvarchar(max)
set @query = 'select * from mytable 'if(@ParameterValueIS = 1)
begin
set @query = @query +' where id=1'
endif(@ParameterValueIS = 2)
begin
set @query = @query +' where id=2'
endexec (@query)
I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.
-
Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,
Agh
mehrdadov wrote:
I'm looking for a solution that let me depend on the situations make my select query with or without joins.
Let me rephrase that, to make sure I understood you correctly; you're looking for something simpeler than a join-statement?
Bastard Programmer from Hell :suss:
-
Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,
Agh
mehrdadov wrote:
this is simple but when I have many situation and more tables to join it grows a lot.
Why are you doing this? As noted in the other reply one can create a query dynamically. A client app could do this as well. But you really shouldn't have "many" of these. It should normally be limited to a very few cases.