Nested if statements in SQL Server stored procedure SELECT statement -
I'm new here, and relatively new to stored functionality, so please interact with me! I have examined the related questions here and there is no work to do in this example.
I am trying to create a stored procedure (MS SQL Server 2005) which takes the number passed in values, actually makes SQL dynamic, as you would with inline SQL.
This is where I am unstable.
We have some (simple to clarify):
@ searchf1 varchar (100), - Search filter 1 @ Search 1 varchar (100), - Search Results 1 @ Search 2 varchar (100), - Search filter 2 @ Search 2 varchar (100), - - Search result 2 @ direction characters (1), - Set the order to set date to date set to date * Select from nocount. Database.dbo.table T WHERE T.deleted = 'Order' Directions by Order 'When' A 'THEN TDID WHEN' D 'THEN TDID DESC End End Set Knockout
I Also have tried the lines by ORDER BY:
if @direction = 'n' then TDi DESC by ordering from TDID, both
Attitudes give me an error on lines:
"Wrong syntax near the keyword 'DESC'." (The line ID references DESC based on the last command
As part of this archived process, I want to try feeding in matching pairs of values that will be used to view the area To render an area, and to match it to the field, they can either be present or "." To do this, add me to the SELECT section code like this. Will be deleted:
deleted WHERE = 'n' IF @ searchf1 <<
"and then fieldf 1 = @ search1 and fielder1 = @ < / Pre>However, such errors arise:
Wrong syntax near the keyword 'IF'.
I know that this type of dynamic SQL is not the most beautiful And I know that I can do it through global if ISE points, but if I gave thousands of rows to SP, then up to 15 The pair of these search fields with the direction and field to order.
(The current version of this SP uses a pass in the list of IDs for some inline dynamic return generated by SQL. Trying to generate it in a hit)
Any help is greatly appreciated I have simplified the code for clarity in the above example, because it is very simple They have went general concept SELECT and ORDER with a nested IF statement'm about which I ask.
For this I try to go with a more formal dynamic SQL solution, which is defined by you Input parameters are given, select from
DECLARE @SQL VARCHAR (MAX) SET @SQL = 'database.dbo.tableTWERE T.deleted =' 'n' '' - If your Conditional Goods are here, you can find out if @ & Gt; '' Then SECL = @SQL + '' and FieldF 1 = '+ @ Search1 +' and fielder 1 = '+ Search1 +' '- query SET @ SQL = @SQL +' Order by X '(SSL) < / Code>
Disclaimer: The use of dynamic SQL is not something that should be taken lightly, and should be considered appropriate under all circumstances To make sure that you are not open for SQL injection attacks, however, some dynamic search queries Government is the most popular route for the operation.
Comments
Post a Comment