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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 stored procedure for insert

Author  Topic 

maddyslayer
Yak Posting Veteran

57 Posts

Posted - 2013-04-18 : 15:54:13
I have two tables

Product

prod_cd prod_type prod_active
p reg Y
q reg Y
r reg Y
s reg Y
t reg Y
u reg N
v reg N
w irreg Y
x reg Y


Customer

s_id (Pkey) cus_id prod_cd prod_state
1 1 p y
2 1 q y
3 1 v n
4 1 w n
5 2 s y
6 2 p n
7 2 q y
8 3 p n
9 4 q y
10 5 v n


What is the stored procedure to insert new records for a customer and that cus_id should have a record with each prod cd from the product table where prod_type = reg and prod_active = y and prod_state should be set to N?

What is the stored procedure to loop through the existing customer (cus_id) records and check if they have all the prod_cds with prod_active = N or else add the prod_cds with prod_active = N that are not there and insert those records for that cus_id with prod_state set to N?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-19 : 02:32:07
where does the customerid come from? is it user input?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

maddyslayer
Yak Posting Veteran

57 Posts

Posted - 2013-04-22 : 14:31:21
It is not user input, each customer has a unique id with different products
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-23 : 13:05:16
so what should be output for above sample data?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -