dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

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 ...


Home > Database Forum > Microsoft SQL Server > sqlserver-programming > A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 07-25-2007, 09:59 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

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

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

Reply With Quote
  #2  
Old 07-25-2007, 10:13 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

On Jul 25, 9:59 am, Dan Bridgland wrote:
> 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

Reply With Quote
  #3  
Old 07-25-2007, 10:55 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

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


Reply With Quote
  #4  
Old 07-25-2007, 12:11 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

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

Reply With Quote
  #5  
Old 07-25-2007, 01:13 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

>> 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


Reply With Quote
  #6  
Old 08-21-2009, 09:26 AM
Database Newbie
 
Join Date: Jul 2009
Posts: 14
shijobaby is on a distinguished road
Default Re: A SELECT statement that assigns a value to a variable must not be combined with d

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
Reply With Quote
  #7  
Old 11-24-2009, 06:02 AM
Database Newbie
 
Join Date: Nov 2009
Posts: 2
tsqldeveloper is on a distinguished road
Default Re: A SELECT statement that assigns a value to a variable must not be combined with d

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
__________________
T-SQL Developer
SQL Server & T-SQL Programming
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads

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.