0

Can anyone tell me why this doesn't work?

DECLARE
  @ItemList         VARCHAR(max);

IF EXISTS
    (SELECT IM_Data INTO ItemList
      FROM IM_DataTable
      WHERE xRowNum<=1)

Error:

Lookup Error - SQL Server Database Error: Incorrect syntax near the keyword 'INTO'.

2
  • 2
    You've tagged this for Oracle. Your code seems to be a mix of SQL Server and Oracle. And the error message appears to be coming from SQL Server. Which database are you really using? Commented Apr 16, 2013 at 13:57
  • 1
    And why is the IF there?, what is it that you want to do? Commented Apr 16, 2013 at 13:59

4 Answers 4

5

Ok, first of all, you need to make sure that the result from your query is only one row, so I don't see the need for the WHERE xRowNum<=1, you should use xRowNum=1:

DECLARE @ItemList VARCHAR(max);

SELECT @ItemList = IM_Data
FROM IM_DataTable
WHERE xRowNum = 1
Sign up to request clarification or add additional context in comments.

1 Comment

@user1911612 How can you be getting the same error when I'm not using a INTO in my answer?
1

I would think it's expecting ItemList to be a table--you SELECT INTO a table, not a variable.

Syntax in SQL Server is:

SELECT 
   ItemList = IM_Data
FROM 
   IM_DataTable
WHERE
   xRowNum<=1

I'm not sure how the IF EXISTS fits into that. You want to set the variable only if data exists?

2 Comments

Yes. I would like to store it as the variable so I can parse through it after in a function.
The above should work then, although I'm still not clear on what the IF EXISTS bit is for.
0

Why doesn't this work?

Well, the subquery that you have is:

SELECT IM_Data
INTO ItemList
FROM IM_DataTable
WHERE xRowNum<=1

This is perfectly valid syntax for creating a table, called ItemList, in the current database. However, this syntax is not allowed in an if statement.

So you could phrase this as:

SELECT IM_Data
INTO ItemList
FROM IM_DataTable
WHERE xRowNum<=1

if exists (select * from itemlist)

Or you could dispense with the additional table altogether:

if exists (select * from IM_DataTable WHERE xRowNum<=1)

Or, you might have some other intention altogether.

Comments

0

You have left off the @ sign that needs to be at the beginning of the variable name. Also, it seems like you are mixing together the syntax to check for existence and the syntax to retrieve data into a variable. Are you trying to just check for existence of a certain row or rows in IN_DataTable? If so, leave out the "INTO @ItemList" so that you just have the following:

IF EXISTS 
    (SELECT IM_Data
     FROM IN_DataTable
     WHERE xRowNum <= 1)
...

If you need to retrieve the IM_Data and use it later, you should do something like this:

DECLARE @ItemList VARCHAR(max);
SELECT @ItemList = IM_Data 
FROM IM_DataTable
WHERE xRowNum<=1;
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.