sql server - SQL iteration without creating a table? -
I want to run a running SQL query as a way of finding records within a database. The name of the table is long and upset, but I will try to bring it down for a minimum example. I'm searching for a table of cars for cars with some features. Normally, it would be very easy:
< Choose from cars> pre-<<> cars> color = 'red' and internal = 'cloth' However, users do not choose 'no preference' option, in that case I Do not want to filter by attribute. Let me meet with conditional subqueries around this, where @color
is defined by the user input:
Choose from vin cars where (select when @color = 'No Preference' then ID when Color = @ Color THEN id) and ID (Selection case when @Interreure = 'no preference' then id = INTERIOR = @ INDIER THEN ID)
It works because it selects the group of all records when nothing is 'no preference' (which is the same as i and
all Some get together, he will ensure that I do not lose any).
However, it is now necessary to filter several car specifications, and although I can write all of these and then and
I can get together, I think I should be able to do that is a smart fashion, which I think is easy to walk in SQL; Just make a car that has all the car goggles that & lt; & Gt; 'No preference'
and iterate over them.
This works fine with my developer database access, however, CREATE
or DROP
is not allowed in questions made by end user credentials of accounts Therefore, creating a table is not an option for me.
Is there a way I can do this subquire, by creating a new table?
It seems as if you want to create a dynamic SQL statement based on your inputs Is:
announcement @ sqlstmt VARCHAR (MAX) SET @sqlstmt = 'Select from cars where 1 = 1' if the interior < & Gt; No preference 'SET @sqlstmt = @sqlstmt +' and interior = '+ + + + interior +' 'IF @ color & lt; & Gt; No preference SET @sqlstmt = @sqlstmt + 'and color =' '' + @ color '' '... ... exec (@ Sqlstmt)
Obviously, You have to filter by injection, but this argument should start with you.
Comments
Post a Comment