I've never actually done this myself, but try this:
(Note: this example is using T-SQL, this will probably be completely
useless for anything other than Microsoft SQL Server)

DECLARE @sqlstring varchar(128)
SET @sqlstring = 'Select * from #ExtReturn'
IF LEN(@sort) > 0
BEGIN
      SET @sqlstring = @sqlstring + ' Order by ' + @sort  -- note the
spaces around the order by
END
EXEC @sqlstring

If that doesn't work, try removing the single quotes from your last
attempt, and add a space after the word "by".  The way your last attempt
reads, it looks like the resulting SQL was:  exec ("Select * from
#ExtReturn order by'Lname'")
which should throw a syntax error.
|-------------------------+-----------------------------------------------|
|Eric A. Laney            |No battle plan ever survives contact with the  |
|Systems Architect        |enemy.                                         |
|Verizon Security         |                                               |
|Voice: 813.987.1416      |                                               |
|Pager: 888.551.3718      |                      Murphy's 2nd Military Law|
|-------------------------+-----------------------------------------------|





                                                                                       
                                   
                      "Teel, C. Doug"                                                  
                                   
                      <cteel@fulbright.        To:       SQL <[EMAIL PROTECTED]>   
                                   
                      com>                     cc:                                     
                                   
                                               Subject:  Stored Procedure Variable 
Help                                   
                      05/28/2002 06:24                                                 
                                   
                      PM                                                               
                                   
                      Please respond to                                                
                                   
                      sql                                                              
                                   
                                                                                       
                                   
                                                                                       
                                   




I'm trying to pass in a variable like @sort and select out of a temp table
and order by that variable.  I can't get this to work no matter what
combination of quotes and concationations I use.  Any help is greaty
appreciated. I'm lost.


@sort varchar(50)
SET @sort = 'lname'


--bunch of joins and selects here dumped into #extreturn

-- my last attempt
exec ("Select * from #ExtReturn order by'"+ @sort+"'")


this works fine:
exec ("Select * from #ExtReturn order by Lname)

Thanks,
Doug





______________________________________________________________________
Your ad could be here. Monies from ads go to support these lists and provide more 
resources for the community. http://www.fusionauthority.com/ads.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to