Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Database table,

SQL
CREATE TABLE [dbo].[STS_ADDRESS_BOOK](
         [AB_ID] int NOT NULL IDENTITY(1,1) CONSTRAINT primarykey PRIMARY KEY,
         [AB_NAME] [varchar](100),
         [AB_EMAIL] [varchar](100),
         [AB_FAX] [varchar](100),
         [AB_ADDRESS][varchar](max),
         [AB_PHONE][varchar](16)
)

------------------------------------------------------
then sp,


SQL
GO
IF OBJECT_ID ('dbo.sortAddressBookInformation')IS NOT NULL
DROP PROC  sortAddressBookInformation
GO

CREATE PROC sortAddressBookInformation @columnvalue varchar(100)
AS

exec('SELECT * FROM STS_ADDRESS_BOOK ORDER BY' +@columnvalue)

this command executed successfully.but the execution of sp using ,

exec sortAddressBookInformation @columnvalue=[AB_NAME] give error as,

Incorrect syntax near 'BYAB_NAME'.

why?what is the mistake?
Posted
Updated 25-Sep-12 1:36am
v2

hi,
u were missing space after order by
exec('SELECT * FROM STS_ADDRESS_BOOK ORDER BY ' +@columnvalue)
 
Share this answer
 
Comments
Abhijit Parab 25-Sep-12 7:36am    
pass @columnvalue as in varchar like @columnvalue='column name'
hasbina 25-Sep-12 7:41am    
Thank you Abhijit..
The SP has a problem you need to put a Space after the By


IF OBJECT_ID ('dbo.sortAddressBookInformation')IS NOT NULL
DROP PROC sortAddressBookInformation
GO

CREATE PROC sortAddressBookInformation @columnvalue varchar(100)
AS

exec('SELECT * FROM STS_ADDRESS_BOOK ORDER BY ' +@columnvalue)



Try executing this Store Procedure it will work.

'SELECT * FROM STS_ADDRESS_BOOK ORDER BY' +@columnvalue. You missed to put a space after the By clause.

thanks
Happy Coding.
Soumen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900