Link to home
Start Free TrialLog in
Avatar of ipendlebury
ipendlebury

asked on

sp_executeSQL problem

Why doesn't this work in my Stored Procedure:

Select @SqlStatement = 'Select @Updated = Top 1 Updated From ' + @TableName + '  ORDER BY Updated Desc'

exec sp_ExecuteSQl  @SqlStatement, N'@Updated smalldatetime out',  @Updated out

It gives me the error: Incorrect syntax near the word 'Top'.


ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Atlanta_Mike
Atlanta_Mike

Select @SqlStatement = 'Select Top 1 @Updated =  Updated From ' + @TableName + '  ORDER BY Updated Desc'

exec sp_ExecuteSQl  @SqlStatement, N'@Updated smalldatetime out',  @Updated out

It gives me the error: Incorrect syntax near the word 'Top'.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipendlebury

ASKER

Yes that was it. It seems an ilogical way to structure the systax though. Anyway, it's working now. Thanks for your help
>Yes that was it. It seems an ilogical way to structure the systax though

no, because:
select top 1  @var1 = field1, @var2 = field2 etc from table etc...
Ah yes of course! Thanks