0

I can do this and it works (but would like something more simple):

Declare @PropIDs        varchar(50)
Set @PropIDs = '1, 2'

IF OBJECT_ID('dbo.TempProp') IS NOT NULL DROP TABLE dbo.TempProp

CREATE TABLE [dbo].[TempProp](
    [PropCode] [VarChar](3) NULL)

Set @Sql = 'Insert Into TempProp Select PropertyCode From Property where PropertyID In (' + @PropIDs + ')'

Execute (@Sql)

But I wish I could do just this:

Declare @PropIDs
Set @PropIDs = '1, 2'
Select PropertyCode 
Into #TempProp
From Property where PropertyID IN (@PropIDs)

It is the "...Property IN (@PropIDs)" that is giving me trouble. Any suggestions?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Mork Mindy
  • 5
  • 1
  • 2

1 Answers1

0

Create a split table value function similar to this one

create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
returns @temptable TABLE (items varchar(MAX))       
as       
begin      
    declare @idx int       
    declare @slice varchar(8000)       

    select @idx = 1       
        if len(@String)<1 or @String is null  return       

    while @idx!= 0       
    begin       
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
            set @slice = left(@String,@idx - 1)       
        else       
            set @slice = @String       

        if(len(@slice)>0)  
            insert into @temptable(Items) values(@slice)       

        set @String = right(@String,len(@String) - @idx)       
        if len(@String) = 0 break       
    end   
return 
end;

That code is from this question: separate comma separated values and store in table in sql server

Then use it in your query like this:

Declare @PropIDs
Set @PropIDs = '1, 2'
Select PropertyCode 
Into #TempProp
From Property where PropertyID IN (dbo.Slit(@PropIDs, ','))
Community
  • 1
  • 1
Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59