Parameter in select statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gpsharma
    New Member
    • Jul 2010
    • 2

    Parameter in select statement

    Hi,

    I need to pass a parameter into a stored procedure which values are dynamic always. Executing a variable which having the Select statement.


    Following query working fine.

    Code:
    BEGIN
    DECLARE @strQuery NVARCHAR(MAX)
    DECLARE @CountryId INT
    DECLARE @CityId INT
    
    SET @CountryId = 2
    SET @CityId = ' AND CityId IN (23,45,85,86)'
    
    SELECT @strQuery= 'SELECT VendorId From Vendors
    WHERE CountryId = '+@CountryId+' '+@CityId+'
    ORDERBY CreatedDate'
    
    EXEC(@strQuery)
    END
    I need to execute above in an open select statement instead of executing @strQuery.

    Getting error as When trying through following script as "Conversion failed when converting the varchar value ' AND CityId (23,45,85,86)' to data type int"

    Code:
    SELECT VendorId From Vendors
    WHERE CountryId = @CountryId + ' ' +@CityId 
    ORDERBY CreatedDate
    Please help me to get it working through above select statement.

    Thanks

    Sharma
    Last edited by NeoPa; Oct 5 '10, 03:52 PM. Reason: Please use the [CODE] tags provided.
  • gpl
    New Member
    • Jul 2007
    • 152

    #2
    See my answer in this question

    Comment

    Working...