Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql statment

Author  Topic 

issammansour
Yak Posting Veteran

51 Posts

Posted - 2007-08-17 : 07:01:18
hi,

I Have a following SP running on sql 2005:-

DECLARE @MONTHNO int
DECLARE @DB CHAR (8)
DECLARE @CR CHAR (8)
SET @MONTHNO = 2
SET @DB = 'DB_'+CONVERT(NCHAR(2),@MONTHNO)
SET @CR = 'CR_'+CONVERT(NCHAR(2),@MONTHNO)
SELECT (acctno),@CR from bmaster

. The table has a 12 field with cr_1 ... to 12

. the sql can’t understand the @cr the column papers "with No column name'

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-17 : 07:08:09
http://www.sommarskog.se/dynamic_sql.html

exec ('SELECT acctno, ' + @CR + ' from bmaster')



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-17 : 08:40:54
quote:
Originally posted by issammansour

hi,

I Have a following SP running on sql 2005:-

DECLARE @MONTHNO int
DECLARE @DB CHAR (8)
DECLARE @CR CHAR (8)
SET @MONTHNO = 2
SET @DB = 'DB_'+CONVERT(NCHAR(2),@MONTHNO)
SET @CR = 'CR_'+CONVERT(NCHAR(2),@MONTHNO)
SELECT (acctno),@CR from bmaster

. The table has a 12 field with cr_1 ... to 12

. the sql can’t understand the @cr the column papers "with No column name'



Make sure you read Sommarskog's article fully

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -