Dynamic TSQL Error using insert with nested select - boolean expression expected

  • Can anyone tell me what is the right way to code the following. Simply, when I do the insert without the where clause, its works. I add the where clause and i get the following error... thanks

    ------------------

    An expression of non-boolean type specified in a context where a condition is expected, near 'description'.

    ------------------

    SET @SEQ = 'insert into #TABLE_DATA (field_data) '

    SET @SEQ = @SEQ + ' (SELECT TOP 4 '

    SET @SEQ = @SEQ + ' description '

    SET @SEQ = @SEQ + ' FROM '

    SET @SEQ = @SEQ + ' sugarcrm.accounts '

    SET @SEQ = @SEQ + ' where description is not null )' -- this line creates the error....

    exec (@SEQ)

  • shadigirgis (7/7/2009)


    Can anyone tell me what is the right way to code the following. Simply, when I do the insert without the where clause, its works. I add the where clause and i get the following error... thanks

    ------------------

    An expression of non-boolean type specified in a context where a condition is expected, near 'description'.

    ------------------

    SET @SEQ = 'insert into #TABLE_DATA (field_data) '

    SET @SEQ = @SEQ + ' (SELECT TOP 4 '

    SET @SEQ = @SEQ + ' description '

    SET @SEQ = @SEQ + ' FROM '

    SET @SEQ = @SEQ + ' sugarcrm.accounts '

    SET @SEQ = @SEQ + ' where description is not null )' -- this line creates the error....

    exec (@SEQ)

    You know, the people that help out here are all un-paid volunteers. Providing the DDL scripts (CREATE TABLE, CREATE INDEX, etc.) for the tables affected, and INSERT statements to put some test data into those tables that shows your problem will go a long way in getting people to look at your issue and help you out. For your problem, please include the INSERT INTO scripts for the first 4 rows from sugarcrm.accounts where description is not null. Please include code for what you have already tried. If you help us with this information, we'll be able to help you with your problem a lot easier.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Here are the declerations that i use...

    *********************************

    IF EXISTS (SELECT ID FROM TEMPDB.DBO.SYSOBJECTS WHERE ID = OBJECT_ID('TEMPDB.DBO.#TABLE_DATA'))

    DROP TABLE #TABLE_DATA

    CREATE TABLE #TABLE_DATA (

    FIELD_DATA varchar(1000) )

    DECLARE @SEQ VARCHAR(100)

  • Replace exec(@SEQ) with

    PRINT @SEQ

    -- exec (@SEQ) -- uncomment when it's debugged

    Helps a lot.

    _____________
    Code for TallyGenerator

  • You are amazing :-)!!!!!!!!!!!!!!!!!!!!! Thanks!

    I can't believe the error I had. My @SEQ was only varchar(100) and so it was getting cut off after the word description in my where clause...

    I changed the size and it's fixed 🙂 I spent so many hours...

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply