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

  • SEARCH
  • Home
  • 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 7951899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:42:04+00:00 2026-06-04T02:42:04+00:00

ALTER TRIGGER t1 ON dbo.Customers FOR INSERT AS BEGIN TRANSACTION /* variables */ DECLARE

  • 0
ALTER TRIGGER t1
ON dbo.Customers

FOR INSERT
AS

BEGIN TRANSACTION

/* variables */

DECLARE
    @maxid bigint

SELECT @customerid = id FROM inserted

SET IDENTITY_INSERT dbo.new_table ON

DECLARE
    @maxid bigint

SELECT @maxid = MAX(ID) FROM new_table

INSERT INTO new_table (ID, ParentID, Foo, Bar, Buzz)
    SELECT ID+@maxid, ParentID+@maxid, Foo, Bar, Buzz FROM initial_table

SET IDENTITY_INSERT dbo.new_tableOFF

/* execute */
COMMIT TRANSACTION

GO

fails with:

SQL Server Subquery returned more than 1 value. This is not permitted
when the subquery follows =, !=, <, <= , >, >= or when the subquery is
used as an expression

How to fix it?

What I am trying to do is

  • insert id and parentid, each INCREASED by @maxid
  • from initial_table
  • into new_table

thnx

new_table

id (bigint) 
parentid (bigint - linked to id) 
foo | bar | buzz (others are nvarchar, not really important)

initial table

id (bigint) 
parentid (bigint - linked to id)
foo | bar | buzz (others are nvarchar, not really important)
  • 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-06-04T02:42:06+00:00Added an answer on June 4, 2026 at 2:42 am

    You are battling against a few errors I suspect.

    1.
    You are inserting values that violate a unique constraint in new_table.
    Avoid the existence error by joining against the table you are inserting into. Adjust the join condition to match your table’s constraint:

    insert into new_table (ID, ParentID, Foo, Bar, Buzz)
        select  ID+@maxid, ParentID+@maxid, Foo, Bar, Buzz 
        from    initial_table i
        left
        join    new_table N on 
                i.ID+@maxid = n.ID or 
                i.ParentID+@maxid = n.ParentId
        where   n.ID is null --make sure its not already there
    

    2.
    Somewhere, a subquery has returned multiple rows where you expect one.
    The subquery error is either in the code that inserts into dbo.Customer (triggering t1), or perhaps in a trigger defined on new_table. I do not see anything in the posted code that would throw the subquery exception.

    Triggers (aka, landmines) inserting into tables that have triggers defined on them is a recipe for pain. If possible, try to refactor some of this logic out of triggers and into code you can logically follow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ALTER TRIGGER [dbo].[AlertMail_tr] ON [dbo].[MethodToLog_tbl] AFTER INSERT AS BEGIN DECLARE @LogId int SET @LogId
this is my trigger ALTER trigger [dbo].[addpay] on [dbo].[pays] after insert as declare @idtutor
I have below trigger ALTER TRIGGER [dbo].[DeleteUserData] ON [dbo].[site_users] AFTER DELETE AS BEGIN SET
My trigger is as below , Alter TRIGGER [dbo].[LogTable_InsertTrigger] on [dbo].[std_table] AFTER INSERT as
I'm sending a demo trigger CREATE TRIGGER MyTestTrigger ON dbo.tblCursor AFTER INSERT AS Declare
I started with the following trigger in SqlServer 2000 ALTER TRIGGER DeleteTrigger ON [dbo].[WarrantyClaimsLineItems]
I've got a trigger attached to a table. ALTER TRIGGER [dbo].[UpdateUniqueSubjectAfterInsertUpdate] ON [dbo].[Contents] AFTER
I'm having a problem with this trigger: ALTER TRIGGER [dbo].[trg_B_U_Login] ON [dbo].[Login] FOR UPDATE
I have the following trigger to avoid updating a certain column. ALTER TRIGGER [dbo].[MyTrigger]
I have a trigger which is as follows: ALTER TRIGGER [trigger_CATEGORY_VALUE_ID] ON [dbo].[tblA] FOR

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.