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 |
|
missMac
Posting Yak Master
124 Posts |
Posted - 2008-11-17 : 08:36:44
|
Hello Guys,whats the best way to implement this:I have a table that values will be inserted based on a dynamic value, ie if its 100, then 100 will be selected from the parent table, and if its 10,000 then 10,000 will be selected using the union all clause.Problem, is that its a mixture of variables and table values. Here is my code belowdeclare @y int, @x intset @y = 100INSERT myhistory (ID, recipient, Username select while @x <= @y(@senderID, (select recipient from log_history_table where MyID = @ID) ,@user SET @X = @X + 1 Am a bit confused :-(SUMMARY: I want to run a loop or set based approach to get the total values selected into the main table from the log table, alongside with the variablesMM |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-17 : 08:59:13
|
| Not sure what you wantdeclare @y int, @sql varchar(100)set @y = 100INSERT myhistory (ID, recipient, Username) SELECT @senderID, recipient, @user from log_history_table, master..spt_valuesWHERE MyID = @ID and type='p' and number<@yMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-17 : 09:41:47
|
| [code]INSERT myhistory (ID, recipient, Username)SELECT @senderID, recipient, @user from log_history_table WHERE MyID = @IDGO 100[/code] |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-11-17 : 13:49:08
|
quote: Originally posted by visakh16
INSERT myhistory (ID, recipient, Username)SELECT @senderID, recipient, @user from log_history_table WHERE MyID = @IDGO 100
declare @counter intset @counter = select count(*) from myhistory where myid = @idINSERT myhistory (ID, recipient, Username)SELECT @senderID, recipient, @user from log_history_table WHERE MyID = @IDGO @counterBut if you have multiple values of myid, then it will run that number of times, times the count value.Alternatively, you can omit the go HenceINSERT myhistory (ID, recipient, Username)SELECT @senderID, recipient, @user from log_history_table WHERE MyID = @IDCause it will loop already. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-18 : 04:11:23
|
quote: Originally posted by afrika
quote: Originally posted by visakh16
INSERT myhistory (ID, recipient, Username)SELECT @senderID, recipient, @user from log_history_table WHERE MyID = @IDGO 100
declare @counter intset @counter = select count(*) from myhistory where myid = @idINSERT myhistory (ID, recipient, Username)SELECT @senderID, recipient, @user from log_history_table WHERE MyID = @IDGO @counterBut if you have multiple values of myid, then it will run that number of times, times the count value.Alternatively, you can omit the go HenceINSERT myhistory (ID, recipient, Username)SELECT @senderID, recipient, @user from log_history_table WHERE MyID = @IDCause it will loop already.
GO @counter wont work. You need dynamic sqlMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|