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 |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2009-06-11 : 11:21:18
|
| I have a sp which job is to essentially take a look at a list of individuals product stored in the product_Master table, and insert a random individual product into the product_Pool_Detail table. The constraints is the individuals selected in the product Master table can not have previously been selected and they cannot be related to by either Last Name or in the same Street Name and City. Right now , the SP generates a random product one by one..Beacuse I won;t know unitle the current row got inserted, the next row won;t know whether the record got already exists or not.... buti am trying to do this in a bulk insert not one by one ...I am trying to create a looping logic that I can pass the parameter( let's say I want to generated 300 random products ) , so I can pass two parameters (Product Pool ID and number needed to be pulled) and generate the entire list all at once. The key being as random as possible. The each records shouldn't be the same based on the last name or Street Name and City and previously not selected..how can I do this? ALTER PROCEDURE [dbo].[usp_TEST] @product_Pool_ID int = 1ASBEGINSET NOCOUNT ON;Declare @Chosen int = 0Declare @Product_ID int = 0Declare @Default_Per_Diem money = 0Declare @Default_Mileage_Rate money = 0SELECT @Default_Mileage_Rate = pro_Master.Default_Mileage_Rate, @Default_Per_Diem = pro.Default_Per_DiemFROM Product_Pool_Master INNER JOINpro_Master ON Product_Pool_Master.C_ID = pro_Master.C_IDWHERE (Product_Pool_Master.Product_Pool_ID = @Product_Pool_ID);WITHDataAS(Select Product_ID,ROW_NUMBER() Over(Order By Product_Last_Name, Product_First_Name) as myrownumber,ROUND(((COUNT(*)OVER() - 2) * RAND() + 1), 0) chosenFrom Product_MasterWhere Product_Served_Status = 0AND NOT EXISTS(SELECT*FROMdbo.vw_ProductPool_Family_ListWHEREvw_ProductPool_List.Product_Pool_ID = @Product_Pool_IDAND vw_ProductPool_List.Name = product_Master.product_NameOR ( RTRIM(vw_ProductPool_List.Street_Name) = RTRIM(product_Master.Address_Street_Name) AND RTRIM(vw_ProductPool_List.City) = RTRIM(product_Master.Address_City) )))SELECT@Product_ID = Product_IDFROMDataWHEREmyrownumber = chosenInsert Into Product_Pool_Details(Product_Pool_ID, Product_ID, Product_Status_ID, Mileage_Rate, Per_Diem)Values (@Product_Pool_ID, @Product_ID, 2, @Default_Mileage_Rate, @Default_Per_Diem)Update Product_MasterSet Product_Served_Status = 1Where Product_ID = @Product_IDEND |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-11 : 12:55:29
|
| if you want to do bulk insert and at the same time make sure you dont include more than 1 row with related Last Name or Street Name and City then isnt it enough to group by these and take a random product from each group? if you can post some table data, we'll be able to suggest much more clearly |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2009-06-11 : 15:55:36
|
For example, if i get the list of products like below ID List name Adress city random_Num1002 shampoo 113 West New york 1003 shampoo 458 NW New jeery 10113 conditioner 134 street Potland 10034 conditioner 134 street portlandEtc How can I randomely pick 200 unique records where list name is not the same or adress and city is not the same..the possible output could look like The result set could be something like this..the key is it has to be as randome as possible and has to be uique based on the list name or address + cityID List name Adress city random_Num1002 shampoo 113 West New york 10113 conditioner 134 street PotlandEtc…200 records |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-14 : 02:48:42
|
yup..its possible. just use something likeSELECT ID,[List name],Adress,cityFROM(SELECT ROW_NUMBER() OVER (PARTITION BY [List name] ORDER BY NEWID()) AS Seq,* FROM Table)tWHERE Seq=1 |
 |
|
|
|
|
|
|
|