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.
Expand|Select|Wrap|Line Numbers
- 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
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"
Expand|Select|Wrap|Line Numbers
- SELECT VendorId From Vendors
- WHERE CountryId = @CountryId + ' ' +@CityId
- ORDERBY CreatedDate
Thanks
Sharma