Facing Some Problem In Instead Of Trigger For MultiRow Insert

Sep 5, 2006

This is the Trigger which is not working properly during Update, no any
record is going to be updated so pls help.

I am updating the requisition table when any update in quantity in
podetails table

CREATE TRIGGER trig_updateRequistion ON ERP.DBO.TranPurchaseOrderDetail


INSTEAD OF UPDATE

AS

IF UPDATE(Quantity)

BEGIN

Update RequisitionSlipDetail
set RequisitionSlipDetail.PoQuantity =

(Select PoQuantity from RequisitionSlipDetail where
ItemCode=(Select CAST(i.ItemCode as nvarchar(20)) from inserted as i)
and
RSlip_No=(Select CAST(i.RSlip_No as int) from inserted as i)
)
-

((Select Quantity from TranPurchaseOrderDetail where Purchase_OrderNo
=
(Select CAST(i.Purchase_OrderNo as nvarchar(20)) from inserted as i))

- (Select CAST(i.Quantity as int) from inserted as i))

where RequisitionSlipDetail.ItemCode = (Select CAST(i.ItemCode as
nvarchar(20)) from inserted as i) and RequisitionSlipDetail.RSlip_No =
(Select CAST(i.RSlip_No as int) from inserted as i)



Update TranPurchaseOrderDetail set
TranPurchaseOrderDetail.Quantity =
(Select CAST(i.Quantity as int) from inserted as i)
where TranPurchaseOrderDetail.Purchase_OrderNo = (Select
CAST(i.Purchase_OrderNo as nvarchar(20)) from inserted as i)
and
TranPurchaseOrderDetail.ItemCode = (Select CAST(i.ItemCode as
nvarchar(20)) from inserted as i)
and
TranPurchaseOrderDetail.PurchaseDetailId =
(Select PurchaseDetailId from TranPurchaseOrderDetail where
Purchase_OrderNo = (Select CAST(i.Purchase_OrderNo as nvarchar(20))
from inserted as i))

END

View 1 Replies


ADVERTISEMENT

Trigger To Handle Multirow Updates

Mar 2, 2006

I am trying to implement trigger which can handle multirow updates and which is running on replicated table. So I want it never fails as trigger failure brakes replication.

So:

CREATE TRIGGER on_person_update

BEGIN

-- create temp table

-- populate temp table with Inserted values (I do not need Deleted as PK never change)

COMMIT TRAN

-- Am I right that this insures updates on replicated table will never be rollback after this commit?

BEGIN TRAN A

-- Make a checkpoint here to be able to rollback at any time to this point if something wrong inside loop.

SAVE TRAN MyTran

-- Start looping in temp table

-- RUN DML statement to make neccesary changes for each record in temp table

-- Does it make any sense to do this (IF @ERR below)? When I am trying in DML insert string value into integer column it never gets to IF statement - terminates straight away.

-- Reason why I think I need it as this trigger might be called by another trigger and top level trigger will get an error and can make a decision based on this.

IF @ERR <> 0
BEGIN
ROLLBACK TRAN MyTran
RAISERROR('Insert or Update failed in on_person_sls_update trigger with error: %s', 16, 1, @ERR)
RETURN
END

-- End looping temp

-- Do I need here COMMIT TRAN A or trigger will make commit anyway?

END

Why all of this?

Data changed on distributor and arrive to subscriber as a transaction.

We have a trigger on replicated table which will update replicated table in any way but after that it will update another database on subscriber.

This trigger should be able to handle multirow updates.

When this trigger updates another database it runs DML which fires other triggers so they become nested, if I am right. Our trigger should always accept changes from distributor as if it fails replication brakes but after data saved in temp table none or all changes have to be made.

May be I am copmpletely wrong with this template - hope somebody will help.

Thank you,



Igor



View 2 Replies View Related

Multirow Insert Statement,... How?

Mar 19, 2004

In MySQL I was able to insert multiple rows into the db like this:

REPLACE INTO Products (productid, price) VALUES (1, 55), (2, 88), (3, 99);


In transact-Sql (Sql Server 2000), this "REPLACE" keyword means something different. What it meant in MySQL is that if there is an existing row that has the same primary key value as one of the new rows being inserted, it will replace that old row with the new row.

How do I do this in SQL Server 2000?

Also, I can do this in MySQL:

INSERT INGORE INTO Products (productid, price) VALUES (1, 55), (2, 88), (3, 99);


This meant in MySQL is that if there is an existing row that has the same primary key value as one of the new rows being inserted, it will ignore that new row's insert and keep the values of the old row.

How do I do that also in SQL Server 2000?


Thanks

View 15 Replies View Related

Multirow Triggers

Mar 23, 2007

Hi,

I am a sql near novice and have a problem in a project I am working on. The exact details of the tables would be too confusing to explain here so I'll use a hypothetical case,

I have a table with 4 columns reccid (int,pk), apples (int),pears(int),oranges (int), lemons (int), totfruit (int), totcitrus(int)

I have a trigger that whenever I update apples,pears,oranges or lemons columns the totfruit and totcitrus are updated with the totals.
---------------------
after update
as
BEGIN
SET NOCOUNT ON;
update fruit set
totcitrus=lemons + oranges,
totfruit=apples+pears+lemons+oranges
END
---------------------

This works fine for single row updates, but if I run a stored procedure against the table such as;

update fruit set lemons=lemons*2

Then I ger an error message of Subquery returned more than 1 value.

I know I havent written the trigger correctly for multirow updates but dont understand enough to see what I have to do, any advice would be very welcome, also can anyone recommend a suitable book.

Thanks a lot.

View 8 Replies View Related

Trigger - Require Help For Updating A Trigger Following An INSERT On Another Table

Oct 30, 2007

Table 1





First_Name

Middle_Name

Surname


John

Ian

Lennon


Mike

Buffalo

Tyson


Tom

Finney

Jones

Table 2




ID

F

M

S

DOB


1

Athony

Harold

Wilson

24/4/67


2

Margaret

Betty

Thathcer

1/1/1808


3

John

Ian

Lennon

2/2/1979


4

Mike

Buffalo

Tyson

3/4/04


5

Tom

Finney

Jones

1/1/2000


I want to be able to create a trigger that updates table 2 when a row is inserted into table 1. However I€™m not sure how to increment the ID in table 2 or to update only the row that has been inserted.

View 17 Replies View Related

Trigger - Require Help For Updating A Trigger Following An INSERT On Another Table

Feb 5, 2008

A





ID

Name


1

Joe


2

Fred


3

Ian


4

Bill


B





ID


1


4

I want to be able to create a trigger so that when a row is inserted into table A by a specific user then the ID will appear in table B. Is it possible to find out the login id of the user inserting a row?

I believe the trigger should look something like this:

create trigger test_trigger
on a
for insert
as
insert into b(ID)

select i.id
from inserted i
where
--specific USER

View 9 Replies View Related

MultiRow Select To SingleRow Output?

Feb 26, 2004

Here is what I want to do.

I have a customer table with say id, firstname, lastname. I also have another table called phone number that contains multiple phone numbers based on the customer id (id, customerid, phonenumber).

Ok I want to output a record like this

firstname, lastname, phone1, phone2, phone3

I need one record per customer with as many phone numbers as they may have, I could always limit the numbers to 3 or better yet return a null if there are less then the predefined number of phone numbers.

If I try a simple join I get multiline output which is no good for me.

SELECT firstname, lastname, phonenumber
FROM customer, phone
WHERE phone.customerid = customer.id

Thanks.

View 1 Replies View Related

Interaction Between Instead Of Insert Trigger And Output Clause Of Insert Statement

Jan 14, 2008


This problem is being seen on SQL 2005 SP2 + cumulative update 4

I am currently successfully using the output clause of an insert statement to return the identity values for inserted rows into a table variable

I now need to add an "instead of insert" trigger to the table that is the subject of the insert.

As soon as I add the "instead of insert" trigger, the output clause on the insert statement does not return any data - although the insert completes successfully. As a result I am not able to obtain the identities of the inserted rows

Note that @@identity would return the correct value in the test repro below - but this is not a viable option as the table in question will be merge replicated and @@identity will return the identity value of a replication metadata table rather than the identity of the row inserted into my_table

Note also that in the test repro, the "instead of insert" trigger actually does nothing apart from the default insert, but the real world trigger has additional code.

To run the repro below - select each of the sections below in turn and execute them
1) Create the table
2) Create the trigger
3) Do the insert - note that table variable contains a row with column value zero - it should contain the @@identity value
4) Drop the trigger
5) Re-run the insert from 3) - note that table variable is now correctly populated with the @@identity value in the row

I need the behaviour to be correct when the trigger is present

Any thoughts would be much appreciated

aero1


/************************************************
1) - Create the table
************************************************/
CREATE TABLE [dbo].[my_table](
[my_table_id] [bigint] IDENTITY(1,1) NOT NULL,
[forename] [varchar](100) NULL,
[surname] [varchar](50) NULL,
CONSTRAINT [pk_my_table] PRIMARY KEY NONCLUSTERED
(
[my_table_id] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, FILLFACTOR = 70) ON [PRIMARY]
)

GO
/************************************************
2) - Create the trigger
************************************************/
CREATE TRIGGER [dbo].[trig_my_table__instead_insert] ON [dbo].[my_table]
INSTEAD OF INSERT
AS
BEGIN

INSERT INTO my_table
(
forename,
surname)
SELECT
forename,
surname
FROM inserted

END

/************************************************
3) - Do the insert
************************************************/

DECLARE @my_insert TABLE( my_table_id bigint )

declare @forename VARCHAR(100)
declare @surname VARCHAR(50)

set @forename = N'john'
set @surname = N'smith'

INSERT INTO my_table (
forename
, surname
)
OUTPUT inserted.my_table_id INTO @my_insert
VALUES( @forename
, @surname
)

select @@identity -- expect this value in @my_insert table
select * from @my_insert -- OK value without trigger - zero with trigger

/************************************************
4) - Drop the trigger
************************************************/

drop trigger [dbo].[trig_my_table__instead_insert]
go

/************************************************
5) - Re-run insert from 3)
************************************************/
-- @my_insert now contains row expected with identity of inserted row
-- i.e. OK

View 5 Replies View Related

Facing Problem

Jan 17, 2006

Well I am the MS-SQL user I always face the problem in my application.After some time my application stop working goes in to the notresponding stage. Then I have to KILL some session which are SPID > 51.After this my problem is getting resolved after this I can able tocreate new sessions. Please help me ot with this issues.regards,Shailesh

View 2 Replies View Related

Multiple Insert Call For A Table Having Insert Trigger

Mar 1, 2004

Hi

I am trying to use multiple insert for a table T1 to add multiple rows.

Ti has trigger for insert to add or update multiple rows in Table T2.

When I provide multiple insert SQL then only first insert works while rest insert statements does not work

Anybody have any idea about why only one insert works for T1

Thanks

View 10 Replies View Related

Facing Timeout Error

Apr 26, 2007

I am facing Timeout Error as i am using SQL Server 2005 as a database and in database set pooling property as 0 and RemoteTimeout as 0 also as a front end i am using .net 2005 and in web.config file i set pooling=False but then also i am getting the following error.Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
What can i do then...?

View 3 Replies View Related

Facing Problem While Processing My SSIS Package

Jul 2, 2007

Hi All

I am facing problem while processing my SSIS package

The error is:
"Invalid delimited data. Text qualifier must be followed by a column delimiter (except for the last column)."

The text qualifier is the double-quotes character “. Text fields are supposed to get a double quote at the beginning of the string and one at the end. The column delimiter is the upright bar or pipe character |.

Some of the descriptions in table column have double-quotes embedded within the text string. When SSIS encounters one of these embedded quotes, it thinks that is the end of the text string and expects to find the column delimiter character next. It doesn’t, because the " is embedded between other alphabetic characters, so it raises an error.

For example if my column value looks like Test"String"One

What old DTS doing:
DTS make the embedded " into "". The resulting text string then would look like this:
"Test ""strings"" one".

When running the same data into a database using DTS, DTS recognized automatically the "" in the string and changed it to " so in the target the string looked like this: test "string" one. No problem.

SSIS, however, does not allow and does not support doing this "doubling" of embedded " when you are using " as the text qualifier. So it exports the sample string above like this (I added the column delimiters as well):
|"test "string" one"|.

That then causes the error

Please help me out

T.I.A

View 2 Replies View Related

Facing Problems With Data Bound Footer.

Feb 20, 2008

hi everybody..

I have placed a textbox in a page footer of SSRS which is linked with a
databound text box in the body of the report.. which i intended would
give the report databound footer...
but now the problem is tat they dont show up on every page..
earlier they use to mostly not show up at all
or show up on the first or the last page...

could some1 help.??

once i render it to pdf..
all the page footers cant be seen.

regards.

savio

View 4 Replies View Related

Hi I'm Facing Problem In Adding Values To The Database Sqlserver2k

Jul 9, 2007

 hi everyone,                     I have a form in in that 3columns and 8rows default.. I have to store values for 8 rows at a time in to database..I am working with vs2003, asp.net &  C# behind the code.. so pls send me a code for implementing this.. asap vijai 

View 2 Replies View Related

Problem Facing When Attaching Back File To Database

May 24, 2008

I had taken back up on employee from my office. Office server name is UMASHANKAR. My server name is XYZ.  I had attached that back up file BY RIGHT CLICKING :: SERVICE OBJECT-->BACKUP DEVICES-->NEW BACK UP DEVICE.Now i create database of EMPLOYEE my server XYZ. I had tried to restore EMPLOYEE back file by right clicking EMPLOYEE database-->TASKS-->RESTORE-->FILE OR FILEGROUP-->To Database: EmployeeSource of Restore:From Device: Employee.bak(I had selected from file by browsing)i had selected recent back up.But server name in back up is UmashankarMy server name is XYZwhen i am clicking ok then i am getting this error.Restore failed for Server 'XYZ'.(Microsoft.SqlServer.Smo)Additional information:|--> An exception occured while executing a Transact-SQL statement or batch.       |       --> The media set has 2 media families but only 1 are provided. All members must be provided. All members must be provided. RESTORE DATABASE is terminating abnormally. (Microsoft SQL Server, Error: 3132)Plz solve this problem. I need it urgently

View 6 Replies View Related

SQL 2012 :: Facing Deadlocks During Updates On Heap Table

Mar 5, 2014

Facing deadlock issues in my ETL job .

The driver table , which keeps track of what datamarts ran and for what date range gets updated frequently during the etl run . There can be as many as 250 updates issued on this table in a single second.

Now this table is a heap , and there are no indexes on it .

During these updates , we encounter deadlocks causing the ETL job to fail .

Will adding an index faciltate?

View 4 Replies View Related

Facing Error Of Connection Failed , Check Sqlservr

Jan 29, 2008

Hi i have installed sql server 2000 on xp window and facing error of connection failed, check sql server registration what it mean pleas guide me.


webmaster http://www.GlobalGuideLine.com

View 1 Replies View Related

Insert Trigger For Bulk Insert

Nov 25, 2006

In case of a bulk insert, the “FOR INSERT� trigger fires for each recod or only once?
Thanks,

View 1 Replies View Related

Facing Problem In Increasing Connection String Pool Size Sqlserver

Mar 4, 2005

Hi to all,

I am using a connection string like

data source=RemoteHostName;initial catalog=myDb;password=sa;user id=sa;
Max pool size = 200;

And now strange thing is happening ,, I am receiving error :

Timeout expired. The timeout period elapsed prior to obtaining a connection
from the pool. This may have occurred because all pooled connections were in
use and max pool size was reached

The SqlServer Activity Manager is telling that only 100 connections are pooled, and I guess that the Max pool size is 100, It is not being changed by my Connection string. As I am trying to change the default 100 pool size value to 200.

Huh , So stucked up , how to increase the Max pool size.. Is there any way .

I am getting worrried.

Any help ??

Thx and Regards

View 1 Replies View Related

Insert Trigger - How To

Nov 20, 2006

 I would like to have the value of a field to be set the return value of
System.Web.Security.Membership.GeneratePassword(12,4)
every time a a row is inserted.
Can you guide with this?
Do you have  some similar sample code?
Thank you very much

View 1 Replies View Related

Insert Trigger

Dec 3, 2006

I would like to have the value of a field to be set the return value of
System.Web.Security.Membership.GeneratePassword(12,4)
every time a a row is inserted.
Can you guide with this?
Do you have  some similar sample code?
Thank you very much

View 1 Replies View Related

Insert Trigger

Nov 12, 2001

Should a insert trigger fire my update trigger as well? it fires it automatically after the insert? Is this a bug?

View 1 Replies View Related

Insert Trigger (Help)

Aug 31, 2000

I have to create a insert trigger that calls a stored procedure usp_a
I need to pass two parameters to this stored procedure, value coming from the row inserted...
What is the syntax? Any help will be appreciated?

View 1 Replies View Related

Insert Trigger Help

Jan 14, 2005

Hi.

Replication is not an option for me so I am trying to creat a trigger that will basically copy any new record to another database. I am using an on insert trigger and I get all records from the table inserted to the new db not just the new record.

Any ideas?

Thanks.

Bill

View 4 Replies View Related

Pre-insert Trigger

Apr 26, 1999

I am trying to write a pre-insert trigger. I want a row to be deleted first and then have the new row inserted. The end result is an insert/update statement. If anyone knows how to do this or has a better way, let me know, Please.

View 3 Replies View Related

Insert Trigger

Oct 13, 1998

I am trying to write an insert trigger for the following problem. I have a table that contains a field (amount) that contains currency amount. Each row could be a record from a transaction in a different country. I would like each record to be converted into U.S. currency as it is inserted. So I created an other table that contains that exchange rate. How can I create a trigger that does this?

Tax Detail Table Exchange Table

Account# int Country char
Description char ExchangeRate Money
Amount money
Country char

I am working with MSS 6.5. Any and all help will be greatly appreciated.

Fidencio Peña

View 3 Replies View Related

ON INSERT TRIGGER

Dec 8, 1998

MSSQL 6.5

Is there any way to change some columns on inserted lines in ON INSERT trigger?

My notice: I try to find something about it in SQLBOOKS, but I found nothing.
It's possibly not TRUEEE!!!!! It wants me to grumble.


Thank for your answer.

View 1 Replies View Related

Trigger INSERT Q

Nov 28, 2005

Really simple question - I have the following trigger for the INSERT event for Tbl_B:

CREATE TRIGGER calc_total ON [dbo].[Tbl_B]
FOR INSERT
AS

DECLARE @VATRate AS decimal(19,2)
SET @VATRate = (
SELECT Val
FROM Tbl_Numeric_Refs
WHERE Ref = 'VATRate'
)

UPDATE Tbl_B
SET [Total] = ((@VATRate / 100) * [PriceA]) + [PriceA],
[VATRate] = @VATRateHow can I restrict the UPDATE within the trigger to the record(s) being INSERTed? Or am I over-complicating things, and should use a simple UDF for this instead?

View 2 Replies View Related

Trigger On Insert

Oct 22, 2007

Is it possible to pick up the value that is being inserted within the scope of a trigger? For example, if I were to run the following

INSERT INTO people (firstname, lastname)
VALUES ('George', 'V')

Would it be possible to access these values in a trigger before they are inserted?

CREATE TRIGGER trigger1
ON people
FOR INSERT
AS
IF EXISTS (SELECT 1 FROM table1 WHERE firstname = <the value being inserted>) BEGIN
Print '1'
END
ELSE BEGIN
Print '2'
END
GO

View 4 Replies View Related

SQL Trigger To Run On Insert Only

Nov 15, 2007

Rightio - this is probably a simple question for you SQL afficionados.I have written a trigger to update a master table with a CreateDate field. Simple enough I thought but it updates this field when the existing record is edited as well - not so good.This is how it looks like:CREATE TRIGGER CreateDateON MasterFOR UPDATE ASDECLARE@idMaster intSELECT @idMaster = idMaster from Insertedupdate Masterset CreatedDate = getdate()where idMaster = @idMasterGOWell I know I can write an IF statement that will basically say if the field is not null then only update - fair enough - but is there a simpler way to do this? Is there a way I need to write my CREATE TRIGGER statement that ONLY makes it run when it is a NEW INSERT ONLY?THANKS!

View 8 Replies View Related

Trigger: Before Insert

Jan 27, 2004

Hi, i'm a newbie in writting trigger and stored procedure, need to get help on this:

I cannnot find anyway to run the trigger before insert, anyway to do this?

Thanks.

View 2 Replies View Related

Trigger INSTEAD OF INSERT??

Mar 2, 2004

So... dear friends
I want to insert values to a table without firing the foreign key Constraint.
I think one way is to insert values into the parent table first and then perform the initial insert.
Is this possible with an INSTEAD OF INSERT Trigger or another way? If yes can you make an example? I want to check for dublicate values in the parent table too!!
Thanx!

View 6 Replies View Related

Trigger For Insert

May 4, 2008

I have an EMPLOYEE table and DEPARTMENT table, i have to create one to many relationship where one employee can be in many departments, please guide me

create table employee (e_id INTEGER, e_fname varchar(30), e_lname varchar(30), d_id INTEGER references department(d_id))

create table department (d_id INTEGER, d_name varchar(30))

but here the problem would be while inserting into employee table since i have no primary key... do i need to use trigger to insert the right values.

View 10 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved