how Create sql like Query with dynamic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muthulk2
    New Member
    • Feb 2014
    • 13

    how Create sql like Query with dynamic

    Code:
    ALTER PROCEDURE [dbo].[SelectResultAllDatails]
    
    AS
    BEGIN
    		
    DECLARE @query nvarchar(max), @VALUE NVARCHAR(10)
    
    SET @VALUE = 'D013'
    
    SET @QUERY = 'select * from dbo.ResultsDetails where [Student Index] LIKE ''' + @VALUE + '%''' 
    
    SELECT @query
    
    END
    GO
    Last edited by Rabbit; Jun 29 '14, 07:21 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code or formatted data.

    Use an input parameter. You can read more about creating procedures from microsoft's documentation: http://msdn.microsoft.com/en-us/library/ms187926.aspx. You also shouldn't build a SQL string that way, it's subject to SQL injection attacks.

    Comment

    Working...