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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Problems with query, please help

Author  Topic 

JeffK627
Yak Posting Veteran

50 Posts

Posted - 2010-03-08 : 16:12:51
I can't figure out why this query isn't working. The problem seems to be with the code that populates the string variable @cols, because if I hard code the column names in it works. However, this needs to be dynamic so hard-coding the columns isn't acceptable.

I put in lines to PRINT (@cols) and PRINT @sql), but they aren't working either. Here's the code:

BEGIN

DECLARE @TABLE_NAME varchar(100), @SQL nvarchar(MAX)
DECLARE @cols nvarchar(max), @colname nvarchar(255), @delname nvarchar(255)

SELECT @TABLE_NAME = ObjectTable
FROM DCScorecard.Rules
WHERE [OBJECT] = @OBJECT
AND [SYSTEM] = @SYSTEM
AND Release = 'R9'

--create temporary table containing all column names from object table
SELECT COLUMN_NAME AS colname
INTO #mycols
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TABLE_NAME

--cycle through column names
WHILE EXISTS(SELECT colname from #mycols)

BEGIN
--@delname is used to delete from temp table, @colname is used to build query
SELECT @delname = colname, @colname = CASE colname
--change GPID to [Employee GPID]
WHEN 'GPID' THEN 'SRC.[' + colname + ']' + ' AS [Employee GPID]'
ELSE 'SRC.[' + colname + ']'
END
FROM #mycols

--build string of column names separated by commas
--if @cols is empty, just put in 1st column name with no comma
--otherwise put in a comma, then the column name
SELECT @cols = CASE @cols
WHEN '' THEN @colname
ELSE @cols + ', ' + @colname
END

--delete to prevent infinite loop
DELETE FROM #mycols where colname = @delname

--END

--drop temp table
DROP TABLE #mycols

PRINT(@cols)

--build query string and execute query
SET @SQL =
'SELECT
EXC.RuleID,
RUL.FieldName,
RUL.RuleDescription,
EXC.[Data Source ID], '
+ @cols + '
FROM
R9.Exceptions EXC
INNER JOIN ' + @TABLE_NAME + ' SRC
ON
SRC.RecordID = EXC.RecordID
INNER JOIN
DCScorecard.Rules RUL
ON
RUL.RuleID = EXC.RuleID AND
RUL.Display = ''1''
WHERE
(EXC.[Primary GPID] = ''' + @UserGPID + ''' OR EXC.[Secondary GPID] = ''' + @UserGPID + ''' OR EXISTS(SELECT GPID FROM R9.DataAdmins WHERE GPID = ''' + @UserGPID + ''')) AND EXC.[Object] = ''' + @OBJECT + ''' AND EXC.[System] = ''' + @SYSTEM + '''
ORDER BY EXC.RuleID ASC, SRC.RecordID ASC'

PRINT (@SQL)
--EXEC (@SQL)

END
END


Any help appreciated!

JeffK627
Yak Posting Veteran

50 Posts

Posted - 2010-03-08 : 16:15:51
By the wway, I know that EXEC (@sql) is commented out here.
Go to Top of Page

JeffK627
Yak Posting Veteran

50 Posts

Posted - 2010-03-08 : 17:27:08
Nobody?
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2010-03-09 : 02:16:27
1. observation
DECLARE @TABLE_NAME Nvarchar(100)
2. put after every step a PRINT and see if the result is what you expected
Go to Top of Page

JeffK627
Yak Posting Veteran

50 Posts

Posted - 2010-03-09 : 09:36:19
I have put PRINT commands in several places, but they aren't executing.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-09 : 09:41:51
What are you trying to do with your code?

Madhivanan

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

JeffK627
Yak Posting Veteran

50 Posts

Posted - 2010-03-09 : 10:19:50
There is an existing stored procedure that takes a table name as a parameter and returns a list of exceptions to the business rules for the business object associated with that table. There is a column in all the business object tables named "GPID". The business client wants their reports to display that column with the name, "Employee GPID". We don't want to manually rename that column in all the tables, so we're trying to do it in the stored procedure.

The SP originally had "SRC.*" in the select statement. The code that populates @cols is meant to create a list of the columns names from the table that was passed to the SP so that instead of SRC.* it will be SRC.COLNAME1, SRC.COLNAME2 etc, and when it finds SRC.GPID it will rename it as SRC.[Employee GPID].

Each business object table has different columns and different numbers of columns, so it needs to be done dynamically.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-10 : 01:27:04
This part

SELECT @cols = CASE @cols
WHEN '' THEN @colname
ELSE @cols + ', ' + @colname
END


should be

SELECT @cols = CASE WHEN @cols IS NULL
THEN @colname
ELSE @cols + ', ' + @colname
END


Madhivanan

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

- Advertisement -