A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations. - sqlserver-programming
This is a discussion on A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations. - sqlserver-programming ; Hi Folks, I'm trying to pull data from multiple tables into one report, where case_id is the only constant. only I've run into a few problems, I need to use a variable to create a CSV string and call the ...
![]() |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| |||
| |||
| I'm trying to pull data from multiple tables into one report, where case_id is the only constant. only I've run into a few problems, I need to use a variable to create a CSV string and call the results of the variable with other columns form other tables. Here is the syntax. -------------------------------------------------------------------------------------------------------------------------- DECLARE @ClassList char(134) SELECT @ClassList = COALESCE(@ClassList + ',', '') + CAST(CLASS.CLASS AS varchar(2)) FROM CLASS WHERE CLASS.CASE_ID = LOG_JOB_DETAILS.CASE_ID SELECT @ClassList, CLASS.CLASS, LOG_JOB_DETAILS.RENEWAL_JOB_ID, LOG_JOB_DETAILS.CASE_ID FROM LOG_JOB_DETAILS , LOG_JOB, CLASS WHERE ( LOG_JOB.JOB_ID = LOG_JOB_DETAILS.JOB_ID ) AND ( CLASS.CASE_ID = LOG_JOB_DETAILS.CASE_ID ) --------------------------------------------------------------------------------------------------------------------------- The above syntax produces an error "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations." I know from similar posts that this syntax is incorrect, but I can't work out how to link the variable's where clause to the data retrieval's where clause and how to pull the results from the variable into a data retrieval select statement I'd be extremely grateful for someone to point out my flaws and steer me in the right direction. Please? Thanks in advance Dan |
|
#2
| |||
| |||
|
On Jul 25, 9:59 am, Dan Bridgland > Hi Folks, > > I'm trying to pull data from multiple tables into one report, where > case_id is the only constant. only I've run into a few problems, I > need to use a variable to create a CSV string and call the results of > the variable with other columns form other tables. > > Here is the syntax. > -------------------------------------------------------------------------------------------------------------------------- > DECLARE > @ClassList char(134) > > SELECT @ClassList = COALESCE(@ClassList + ',', '') + > CAST(CLASS.CLASS AS varchar(2)) > FROM CLASS > WHERE CLASS.CASE_ID = LOG_JOB_DETAILS.CASE_ID > --------------------------------------------------------------------------------------------------------------------------- http://milambda.blogspot.com/2005/07...-as-array.html http://milambda.blogspot.com/2006/07...-sql-2005.html |
|
#3
| |||
| |||
|
You are using an unsupported syntax. For some obvious failures, see: http://tinyurl.com/dpdej http://tinyurl.com/apuns For some alternatives, see: www.projectdmx.com/tsql/rowconcatenate.aspx -- Anith |
|
#4
| |||
| |||
|
Thanks Ross and Anith, But I'm not all to familar with this kind of advanced sql, I can follow basic instructions but joining one feature to another has caught me out this time. I need to see where i'm going wrong. can anyone point me in the right direction? Dan |
|
#5
| |||
| |||
|
>> I need to see where i'm going wrong. can anyone point me in the right >> direction? You are wrong in using the following statement: SELECT @ClassList = COALESCE(@ClassList + ',', '') + CAST(CLASS.CLASS AS varchar(2)) FROM CLASS WHERE CLASS.CASE_ID = LOG_JOB_DETAILS.CASE_ID You seem to be missing the table LOG_JOB_DETAILS in your from clause. The other problem is the use of aggregate concatenation that is addressed in the previous posts. -- Anith |
|
#6
| |||
| |||
|
Hi Actually this in the group of small errors consuming time Just have aook on my blog http://sqlerrormessages.blogspot.com...t-assigns.html Happy Programming |
|
#7
| |||
| |||
|
Hello all, Here is a sample which utilizes such a t-sql select method. What is the main difference in here is the aggregated variable type is nvarchar(max) not a limited char type. DECLARE @ClassList nvarchar(max) SELECT @ClassList = COALESCE(@ClassList + ',', '') + name FROM Humanresources.Department order by DepartmentID select @ClassList I hope this helps, Eralper |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| Display Modes | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Date conversion problem... | Database Administrator | sqlserver-programming | 5 | 06-29-2007 12:11 PM |
| Storing data and code in a Db with LISP-like interface | Database Administrator | databases | 54 | 04-13-2006 08:56 PM |
| 25 SQL Commandments | Database Administrator | databases | 1 | 09-09-2005 04:59 PM |
| Sybase FAQ: 1/19 - index | Database Administrator | sybase | 13 | 07-20-2004 12:16 AM |
| SAP BW compared to Essbase/SQLServer/Oracle as a Enterprise Data | Database Administrator | olap | 7 | 02-27-2004 10:55 AM |
All times are GMT -4. The time now is 03:05 PM.




Linear Mode
