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 |
|
gagani
Posting Yak Master
112 Posts |
Posted - 2012-03-22 : 12:50:17
|
| table1orderid product quantity measure_units100 A 1 1100 B 1 1100 C 1 1101 A 1 1101 B 1 1productABCDEthe output i am getting is 100101but the output should be 101 from the list of products in the front end, ifthe user selects multiple products(say A and B) and flag=1, then it should give the output as 101 (that is, the output should only filter the orderids which has got orderid with products A and B only, 100 has 3 products, so it should not come in output)the stored procedure is as followsfor the selection of A,B in the front end,the UF_CSVToTable1 will result in the #ordertest withABALTER procedure [dbo].[test_flag1flag2](@product1 varchar(1000), @Flag varchar(100))As--product csv format to table formatcreate table #ordertest (product varchar(300))insert into #ordertest(product) select surveyor from dbo.UF_CSVToTable1(@product1)select distinct product into #AllProducts from product where product not in (select product from #ordertest)DECLARE @OP_TABLE TABLE (OrderId bigint)if(@Flag=1)Begin--declare @orderid as varchar(50)declare @ProName as varchar(50)declare @c1 as intdeclare @c2 as intdeclare CUR1 cursor for select distinct orderid from table1 open CUR1FETCH NEXT FROM CUR1 into @orderidWHILE (@@FETCH_STATUS = 0)BEGINset @c1 =1declare CUR2 cursor for select distinct product from #ordertestopen CUR2FETCH NEXT FROM CUR2 into @ProNameWHILE (@@FETCH_STATUS = 0)BEGINset @c2 =0select @c2=count(*) from table1 where orderid=@orderid and product = @ProName and product not in (select product from #AllProducts)IF(@c2=0)beginset @c1=0endFETCH NEXT FROM CUR2 into @ProNameENDCLOSE CUR2DEALLOCATE CUR2if((@c1=1) insert into @OP_TABLE(OrderId) values(@orderid)FETCH NEXT FROM CUR1 into @orderidENDCLOSE CUR1DEALLOCATE CUR1Endselect * from @OP_TABLE |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
gagani
Posting Yak Master
112 Posts |
Posted - 2012-03-22 : 17:17:48
|
| flag has nothing to do with the database and stored procedure.please do not consider that flag part anywhere.I am thinking that the problem would be at this line.select @c2=count(*) from table1 where orderid=@orderid and product = @ProName and product not in (select product from #AllProducts)I have also consideredselect @c2=count(*) from table1 where orderid=@orderid and product = @ProName and orderid not in(select orderid from table1 where product in (select product from #AllProducts))but is not giving any result.can you please help in this |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-03-22 : 17:29:14
|
| http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|