Re: Need to make Sort ORDER in SP defined by Input variable

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Steve Kass (skass_at_drew.edu)
Date: 06/26/04


Date: Sat, 26 Jun 2004 13:53:55 -0400


Larry Woods wrote:

>Nope!
>
>If I enter 'ISBN' (w/o quotes) when I execute the SP below, it executes,
>showing the correct number of lines, but I get NO fields displayed.
>
>
What does that mean??? Where are you executing this from?

>Original SELECT line:
>
>SET @SQL = 'SELECT ISBN FROM tblISBNs WHERE ISBN = ''0021465614'' ORDER BY '
>+ @sortorder
>
>If I modify toe SELECT from this to this:
>
>SET @SQL = 'SELECT ISBN FROM tblISBNs WHERE ISBN = ''0021465614'' ORDER BY
>ISBN'
>
>it works fine!!!!!
>
>
These two SET statements appear to be identical if ISBN is the actual
parameter, whether or not quotes are used around the parameter. Run
this, and you'll see that the @sql value is the same in each case.

CREATE PROCEDURE dbo.cbe_ISBNActivityHits
(
@sortorder nvarchar(50)
)
AS
SET NOCOUNT ON
DECLARE @SQL nvarchar(400)
SET @SQL = 'SELECT ISBN FROM tblISBNs WHERE ISBN = ''0021465614'' ORDER BY '
+ @sortorder
select @SQL
SET @SQL = 'SELECT ISBN FROM tblISBNs WHERE ISBN = ''0021465614'' ORDER
BY ISBN'
select @SQL
GO

exec cbe_ISBNActivityHits ISBN
exec cbe_ISBNActivityHits 'ISBN'
GO

drop proc cbe_ISBNActivityHits

In any case, an ORDER BY clause to sort by ISBN makes no difference
here, since all returned rows have the same ISBN value.

SK

>Here is the complete SP:
>
>ALTER PROCEDURE dbo.cbe_ISBNActivityHits
>(
>@sortorder nvarchar(50)
>)
>AS
>SET NOCOUNT ON
>DECLARE @SQL nvarchar(400)
>SET @SQL = 'SELECT ISBN FROM tblISBNs WHERE ISBN = ''0021465614'' ORDER BY '
>+ @sortorder
>EXEC sp_executesql @SQL
>
>
>
>"Pablo" <Pablo@discussions.microsoft.com> wrote in message
>news:BD76972A-DC2F-406A-AC2A-0AECA2DA681C@microsoft.com...
>
>
>>Use Northwind
>>go
>>DECLARE @SQL NVARCHAR(1024)
>>DECLARE @Column NVARCHAR(64)
>>
>>Set @Column='LastName'
>>SET @SQL='SELECT * FROM dbo.Employees ORDER BY '+@Column
>>
>>exec sp_executesql @SQL
>>
>>
>>Pablo
>>
>>
>>"Larry Woods" wrote:
>>
>>
>>
>>>Help.... I'm back!
>>>
>>>
>>>
>.........................................
>
>
>
>



Relevant Pages

  • Re: Full text catalog just not populating
    ... exec sp_defaultdb N'NT Authority\System', N'master' ... means either the network guys in my company who don't know SQL but are admins ... > needs this login to log into SQL Server and you can either add back this ... >> fetching U ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Indexing delay for one row.
    ... script and WAITFOR and varying the delay from 1 to 18 seconds. ... could you confirm your exact version of SQL Server that you are seeing ... It is possible that a change was made to the pooling frequence under SP3 to ... exec sp_fulltext_table 'FTSTable','create','FTSCatalog','FTSTable_IDX' ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Casting to VARCHAR(MAX)
    ... But when you do the EXEC, the statement the EXEC passes to SQL Server looks like ... So the string that gets put into #TEMP1 will be truncated to 8000 characters. ... that will force the whole expression concatenating the strings to return VARCHARand you would get the whole string inserted into the table. ...
    (microsoft.public.sqlserver.programming)
  • Re: storing and searching office docs in SQL
    ... You CAN both store and search the contents of the MS Word ... files stored in an SQL Table's FT-enable IMAGE column, ... FTS CONTAINS or FREETEXT to search the contents of that MS word document: ... exec sp_fulltext_database 'enable' -- only do this once! ...
    (microsoft.public.sqlserver.programming)
  • Re: SqlDataAdapter1.SelectCommand.CommandType= CommandType.StoredProcedure
    ... > kann man beim EXEC PROC keine Parameter beifügen. ... CommandType.StoredProcedure wird intern als RPC Command abgesetzt, ... SQL RPC siehe SQL Server Dokumentation, ... nach der Ausführung ungültig werden. ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)