Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ikhuram
Starting Member
28 Posts |
Posted - 2004-05-26 : 02:50:30
|
| I have a table with a field tmp_id. It has values001100120013 and so onI store the where clause in another temp table's respective columnas ('0011, 0012, 0013') When I useselect tmp_id, tmp_dsc from Table where tmp_id in (select col1 from tmp_col). It does not return any rows. Please tell me how to fix this problemCODE: if exists(select 1 from sysobjects where name = 'sp_test') drop proc sp_testgocreate proc sp_testasbeginset nocount on insert into col values('0012, 0013, 0014, 0015', 'Program1, Program2, Program3')create table #tmp_col( col1 varchar(50), col2 varchar(80))insert into #tmp_col select * from coldelete colselect tmp_id, tmp_dsc from tempwhere tmp_id in (select col1 from #tmp_col) and tmp_dsc in (select col2 from #tmp_col) endgosp_test |
|
|
gates_micro
Starting Member
29 Posts |
Posted - 2004-05-26 : 03:43:18
|
| You IN clause is not right. Since you are checking the IN clause in a single string So its not fetching you the values.WHERE ID IN ('0012,0015,0014,0015'). Use the IN clause as WHERE ID IN ('0012','0015','0014') |
 |
|
|
gates_micro
Starting Member
29 Posts |
Posted - 2004-05-26 : 09:17:39
|
| Here is the way to do it.First compile the function and then the procedure.1) Function which will format the string you need to put in the IN subqueryCREATE FUNCTION FORMATSTRING(@inputstr varchar(30))RETURNS varchar(1000)asBEGINdeclare @offset intdeclare @cur intdeclare @tmp varchar(4)declare @len intdeclare @i intdeclare @inpt varchar(30)declare @val varchar(1000)set @i=0set @inpt = @inputstrset @len = len(@inpt)set @cur=0while(@i<@len)beginset @offset = charindex(',',@inpt,@i)if @offset = 0 beginset @offset = charindex(',',@inpt,@i-1)set @cur=@lenendset @tmp = ltrim(substring(@inpt,@i,@offset))set @val = '''' + @tmp + '''' + ',' + isnull(@val,'')if @cur=@lenbegin--print substring(@val,0,len(@val))breakendset @i = @offset + 1end return substring(@val,0,len(@val))END2)create proc sp_testasbegindeclare @sel_stmt varchar(1000)declare @insert_stmt varchar(1000)declare @create_stmt varchar(1000)declare @del_stmt varchar(1000)set @insert_stmt = 'insert into col values(' + '''0012, 0014, 0016, 0020''' + ',' + '''Prg1, Prg2, Prg3''' + ')'print @insert_stmtexecUTE(@insert_stmt)set @create_stmt = 'create table tmp_col (col1 varchar(50),col2 varchar(80))'print @create_stmtexecUTE(@create_stmt)set @insert_stmt = 'insert into tmp_col SELECT * FROM COL'print @insert_stmtexecUTE(@insert_stmt)set @del_stmt = 'delete col'print @del_stmtexecUTE(@del_stmt)declare @tmp_id varchar(100)set @tmp_id =(SELECT COL1 FROM TMP_COL)declare @tmp_dsc1 varchar(100)set @tmp_dsc1 =(SELECT COL2 FROM TMP_COL)declare @tmp_dsc varchar(100)set @tmp_dsc =(SELECT dbo.FORMATSTRING(@tmp_dsc1))set @sel_stmt ='select tmp_id, tmp_dsc from temp where tmp_id in (' + '' + @tmp_id + '' + ')' + 'and tmp_dsc in (' + '' + @tmp_dsc + '' + ')'--+ ' and tmp_dsc in (select col2 from tmp_col)'print @sel_stmtexecUTE(@sel_stmt)end |
 |
|
|
|
|
|
|
|