| Author |
Topic |
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2004-12-20 : 14:03:04
|
| hi,i am tyring to work with the TOP keyword and assign a variable to it, but it keeps giving me an error.here is my code [CODE]declare @updated int, @quantity intselect @updated = ( select transactionID from transaction_table where userid = 'afrika'select @quantity = (select quantity_1 from items_purchased where userid = 'afrika'beginUPDATE G_planSET state = @updated FROM (SELECT TOP @quantity FROM authors ORDER BY pins) AS pin_numbersWHERE G_plan.pins = pin_numbers.pins[/CODE]endHowever i get an error, when i try to use @quantity instead of TOP 100 I am trying to assign a dynamic number to the quantity to be updatedhow could i achieve thisrgdsAfrika |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-12-20 : 14:16:32
|
using dynamic sql or using set rowcount @quantityGo with the flow & have fun! Else fight the flow |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2004-12-20 : 14:22:56
|
| i read an article in the help manual F1 key. That rowount should not be used with the update statement.Thats why i left it out |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2004-12-20 : 14:33:03
|
| i read an article in the help manual F1 key. That rowount should not be used with the update statement.Thats why i left it out |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-12-20 : 14:34:53
|
well then dynamic is the way to go i guess....Go with the flow & have fun! Else fight the flow |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2004-12-20 : 14:44:26
|
| i read an article in the help manual F1 key. That rowount should not be used with the update statement.Thats why i left it out |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-20 : 14:46:51
|
Afrika, please post the statement that you are referring to as I am unable to find it in SQL Server Books Online. The only thing that I can find is this:quote: The setting of the SET ROWCOUNT option is ignored for UPDATE statements against remote tables and local and remote partitioned views.
The above does not apply to your query.Tara |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2004-12-20 : 14:50:39
|
| hi tduggan, press the F1 KEY and type set rowcount. You get the info belowrgdsAfrikaIt is recommended that DELETE, INSERT, and UPDATE statements currently using SET ROWCOUNT be rewritten to use the TOP syntax. For more information, see DELETE, INSERT, or UPDATE. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-20 : 14:54:41
|
| In order to do this with dynamic sql, you'll have to build the variables and run the query all in one exec statement as the variables are out of scope when you run the query with dynamic sql.Tara |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2004-12-20 : 14:58:33
|
| i built the variables in the 1st statement above.so how do i run it ?declare @quantity intselect @quantity = (select....)exec ... ?please advice |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2004-12-21 : 06:03:57
|
| hii got it solved, and posting it here just in case anyone else needs it.rgdsAfrikaDECLARE @Limit INTEGERDECLARE @SQL VARCHAR(2000)SET @Limit = 100 -- For example, make this a query assignmentSET @SQL = 'SELECT TOP ' + @Limit + ' * FROM authors ORDER BY pins'EXEC @SQL |
 |
|
|
|