Re: SELECT TOP with variable not working
From: Paul (Paul_at_discussions.microsoft.com)
Date: 09/07/04
- Next message: wei xiao [MSFT]: "Re: Page (1:368132)"
- Previous message: Steve Zimmelman: "Re: Storing Images"
- In reply to: David G.: "Re: SELECT TOP with variable not working"
- Next in thread: Tom Moreau: "Re: SELECT TOP with variable not working"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 7 Sep 2004 14:01:02 -0700
Hi thanks for the additional information. Was not aware of the sp_exeutesql
command but looks to be pretty powerful and has additional capability over
EXEC.
"David G." wrote:
> Paul wrote:
> > Or just put the dynamic SQL statement in an nvarchar variable and
> >> execute it using sp_executesql instead inside the stored procedure.
> >
> > Hi Dave, just wondering if you could provide a brief example as most
> > the SQL stuff I have done is just interfacing with stored procedures.
> > Thanks. "David G." wrote:
>
> Sure. Here's an example of a stored procedure that can accept a variable
> that determines the number of TOP X rows to return:
>
> Create Proc MyTest
> @iTopX int
> as
> Declare @nvcSql nvarchar(1000)
>
> Set @nvcSql = N' Select TOP ' + CAST(@iTopX as nvarchar(10)) + N'
> ColList From MyTable'
>
> Exec sp_executesql @nvcSql
> Go
>
> Another nice feature of sp_exeutesql is the ability to use parameterized
> queries with it 9see BOL for more information).
>
> You can certainly execute the SQL with just the EXEC command inthe
> stored procedure, but I prefer using sp_executesql, especially since I
> use it often with parameters (not supported by Exec).
>
>
>
>
> --
> David G.
>
>
- Next message: wei xiao [MSFT]: "Re: Page (1:368132)"
- Previous message: Steve Zimmelman: "Re: Storing Images"
- In reply to: David G.: "Re: SELECT TOP with variable not working"
- Next in thread: Tom Moreau: "Re: SELECT TOP with variable not working"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|