Help with (easy for you) query

  • I have this query, but I can't get it to work.

    The error I get is

    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

    declare @cnt smallint

    set @cnt =

    select *, glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    (

    select pordnmbr from lpb..pm30200 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm20000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm10000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    )

    and glaccount not in (select actnumst from lpb..gl00105)

    IF @cnt > 1

    BEGIN

  • What are you trying to set @cnt to?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I would advice you to use Aliases for the tables and then run the query.. probabaly some outer query is accessing the columns of the inner query..

  • ColdCoffee (4/26/2011)


    I would advice you to use Aliases for the tables and then run the query.. probabaly some outer query is accessing the columns of the inner query..

    It's likely this piece

    set @cnt = select *, glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Hi Gail

    I need to email notification if there are any records returned from that query.

    So once the top part is working I need to do something like -

    If @cnt > 1

    BEGIN

    EXEC msdb.dbo.sp_send_dbmail

  • Try this little change:

    Instead of:

    declare @cnt smallint

    set @cnt =

    select *, glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    Do

    declare @cnt smallint

    Select @cnt = count(*)

    , glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • The original query works fine -

    This statement -

    select distinct glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    (

    select pordnmbr from lpb..pm30200 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm20000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm10000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    )

    and glaccount not in (select actnumst from lpb..gl00105)

    I added the cnt part so I could send an email if the record count is greater then zero

  • SQLRNNR -

    I made your changes and got the error

    Msg 102, Level 15, State 1, Line 8

    Incorrect syntax near 'EM'.

  • When trying to set a variable, you have to be careful in whether you use set or select. You cannot call multiple columns in the Set, where in the select you can.

    And actually, you should declare a variable for each column in this case if you want multiple columns in the same statement since data retrieval and variable assignment can't be combined in that manner.

    declare @cnt int

    ,@glaccount varchar(25)

    ,@ErrorReason varchar(50)

    set @cnt = count(*),@glaccount = glaccount, @errorreason = 'ACCOUNT NOT FOUND'

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • krypto69 (4/26/2011)


    SQLRNNR -

    I made your changes and got the error

    Msg 102, Level 15, State 1, Line 8

    Incorrect syntax near 'EM'.

    Post the entire query you just ran to get that error.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • There's no need to count all the rows just to see if any rows exist. It's inefficient.

    IF EXISTS (select 1

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    (

    select pordnmbr from lpb..pm30200 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm20000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm10000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    )

    and glaccount not in (select actnumst from lpb..gl00105))

    BEGIN

    -- Rest of query here

    END

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (4/26/2011)


    There's no need to count all the rows just to see if any rows exist. It's inefficient.

    Good point

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • krypto69 (4/26/2011)


    The original query works fine -

    This statement -

    select distinct glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    (

    select pordnmbr from lpb..pm30200 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm20000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm10000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    )

    and glaccount not in (select actnumst from lpb..gl00105)

    I added the cnt part so I could send an email if the record count is greater then zero

    Use @@ROWCOUNT to obtain the number of rows returned if you intend to use any of the returned data. Otherwise, use Gail's suggestion IF EXISTS.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • declare @cnt smallint

    Select @cnt = count(*)

    , glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    (

    select pordnmbr from lpb..pm30200 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm20000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm10000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    )

    and glaccount not in (select actnumst from lpb..gl00105)

    --print @cnt

    Error is:

    Msg 141, Level 15, State 1, Line 11

    A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

  • krypto69 (4/26/2011)


    declare @cnt smallint

    Select @cnt = count(*)

    , glaccount, 'ACCOUNT NOT FOUND' ErrorReason

    from dynamicsgp_utilities.dbo.V_sfvouchers

    where (sfid like 'EM-%' or sfid like 'EXP-REP%') and sfid not in

    (

    select pordnmbr from lpb..pm30200 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm20000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    UNION select pordnmbr from lpb..pm10000 where doctype = 1 and (pordnmbr like 'EM-%' or pordnmbr like 'EXP-REP%')

    )

    and glaccount not in (select actnumst from lpb..gl00105)

    --print @cnt

    Error is:

    Msg 141, Level 15, State 1, Line 11

    A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

    Yup - look at my follow-up post (right after the post containing that script) and also look at Gails follow up post.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 15 posts - 1 through 15 (of 29 total)

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