Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:18:29+00:00 2026-05-30T20:18:29+00:00

Please help, Actually my query is OK, but the problem is when after inserting

  • 0

Please help,

Actually my query is OK, but the problem is when after inserting the 9th record out of 10 records it says that there is an error on the 10th record, but if i run the query separately; 1st query have 9 records then the 2nd query has only 1 and both has no error.

below is the query that i used:

DECLARE @tblLkUp    VARCHAR(MAX),
        @tblLkUpCol VARCHAR(MAX),
        @strSQL     VARCHAR(MAX)

SET @tblLkUp = 
    (SELECT DISTINCT TOP 1 t.name AS TableName 
     FROM sys.tables t JOIN sys.indexes i ON i.index_id = t.schema_id  
     WHERE t.name LIKE '%$POS Lookup')

SET @tblLkUpCol = 
    (SELECT DISTINCT TOP 1 t.name AS TableName 
     FROM sys.tables t JOIN sys.indexes i ON i.index_id = t.schema_id  
     WHERE t.name LIKE '%$POS Lookup Columns')


SET @strSQL = 'IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUp) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'') = 0
                    BEGIN
                        INSERT INTO ' + QUOTENAME(@tblLkUp) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''Zoom Trans. SPO Line Details'', '''', 
                        ''ZOOM'', ''1'', ''99008981'', ''2'', ''10'', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''', '''', ''0'', ''0'', 
                        ''0'', ''0'', ''0'', ''0'', '''', ''1'', '''', '''', '''', '''', '''', ''0'', ''0'', ''0'', '''', '''', ''0'', ''0'', 
                        ''0'', ''0'', '''', '''', ''0'', ''0'', ''0'', ''0'', ''0'', ''0'', ''0'', ''0'')

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''1'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''1'', ''2'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''2'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''2'', ''5'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''3'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''3'', ''10'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''4'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''4'', ''10012700'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''5'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''5'', ''10012701'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''6'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''6'', ''10012702'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''7'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''7'', ''10012703'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''8'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''8'', ''10012704'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''9'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''9'', ''10012705'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END

                        IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUpCol) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'' AND [Table No_] = ''99008981'' AND [Column No_] = ''10'') = 0
                            BEGIN
                                INSERT INTO ' + QUOTENAME(@tblLkUpCol) + ' VALUES(DEFAULT, '''', ''ZOOMSPODTL'', ''99008981'', ''10'', ''10012750'', ''0'', ''0'', '''', ''0'', ''0'', '''', ''0'', ''0'', ''0'', '''')
                            END
                    END'
EXEC (@strSQL)

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:18:31+00:00Added an answer on May 30, 2026 at 8:18 pm

    The string you concatenate is truncated to 4000 characters. To fix this you can add cast('' as varchar(max)) first in the assignment to @strSQL.

    SET @strSQL = cast('' as varchar(max)) + 
                  'IF (SELECT COUNT(*) FROM ' + QUOTENAME(@tblLkUp) + ' WHERE [Lookup ID] = ''ZOOMSPODTL'') = 0
                        BEGIN
                            INSERT INTO .......
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me with a query. Assume that we have a table with columns:
Could all those CORBA experts out there please help me with this one. I
Please help! I couldn't figure it out how to map the following situation: I
Please help me understand how the process goes. I understand that web browsers contain
Could someone please help me with byte ordering regarding floating point variables? Actually the
Could someone please help and tell me how to use protocol buffers. Actually I
am a noob at universal apps so please help me out and let me
Can you please help me to solve the following problem. I am running a
Would you please help me to find out better solution for the following issue...
I am very new to perl so please help me out in following I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.