Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

Friday, March 30, 2012

Multiple Join Query Problem

I am trying to update a field in TblA based on the contents of a field in
TblB where 4 conditions match but I can't get it to work. ere is what I
tried:
Update TblA t Set [Show Name] =
(Select Contest From TblB s
Where s.Date = t.Date,
s.RegionalCode = t.Regional,
s.unit = t.unit,
s.PF = t.[P or F])
How should that be coded?Update TblA Set [Show Name] =
(Select Contest From TblB s
Where s.Date = TblA.Date and
s.RegionalCode = TblA.Regional and
s.unit = TblA.unit and
s.PF = TblA.[P or F])|||Thanks. I was off on the wrong track.
Wayne
<markc600@.hotmail.com> wrote in message
news:1144491686.935456.163560@.v46g2000cwv.googlegroups.com...
> Update TblA Set [Show Name] =
> (Select Contest From TblB s
> Where s.Date = TblA.Date and
> s.RegionalCode = TblA.Regional and
> s.unit = TblA.unit and
> s.PF = TblA.[P or F])
>|||You might want a where clause in your query:
Update TblA Set [Show Name] =
(Select Contest From TblB s
Where s.Date = TblA.Date and
s.RegionalCode = TblA.Regional and
s.unit = TblA.unit and
s.PF = TblA.[P or F])
where exists
(Select Contest From TblB s
Where s.Date = TblA.Date and
s.RegionalCode = TblA.Regional and
s.unit = TblA.unit and
s.PF = TblA.[P or F])sql

Monday, March 26, 2012

multiple insert call for a table having insert trigger

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

ThanksLooks like SQL Server is treating these multiple inserts as a batch and therefore only assuming one insert.

Try using the GO statement between the inserts and this will cause your trigger to fire with every insert.

INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO

Cheers|||Thanks for reply me back..
I am using cursor to call multiple insert inside that curser.. here is the code..

OPEN DestinationIDList
FETCH NEXT FROM DestinationIDList INTO @.DestinationID

WHILE @.@.FETCH_STATUS = 0

BEGIN

INSERT INTO Table(ID, DestinationID) VALUES(@.ID, @.DestinationID)

FETCH NEXT FROM DestinationIDList INTO @.DestinationID
END
CLOSE DestinationIDList
DEALLOCATE DestinationIDList

What should I have to do so that trigger fire for each insert ?

Thanks

Originally posted by aldo_2003
Looks like SQL Server is treating these multiple inserts as a batch and therefore only assuming one insert.

Try using the GO statement between the inserts and this will cause your trigger to fire with every insert.

INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO
INSERT INTO T1 -- ETC
GO

Cheers|||What you could do is to write the Insert as a stored procedure.

Then call this proc from your cursor passing the variables that you have defined in your cursor

something like :

create proc Insert_T1
(@.id as int , @.DestinationID as int)
as

INSERT INTO T1(ID, DestinationID) VALUES(@.ID, @.DestinationID)
go

All you do call this proc in your cursor land this should make your trigger fire for every insert you perform

Give it a go and see if it works

Cheers|||No Luck :-(|||Can you post the code for your trigger

Cheers|||Here is the trigger's code

CREATE TRIGGER TR_TD
ON dbo.TRHistory FOR INSERT

AS

DECLARE @.ID int
DECLARE @.DSTID int
DECLARE @.RT decimal (18,4)
DECLARE @.Time datetime
DECLARE @.GTID int
DECLARE @.GTExist int
DECLARE @.intErrorCode INT
SET @.GTID = 0
SET @.GTExist = 0
SET @.ID = 0

-- CHECK ID' S VALIDITY

SELECT @.ID = i.ID, @.DSTID= i.DSTID,
@.RT = i.RT, @.Time = i.Time
FROM Inserted i
INNER JOIN ITSPS ON ITSPS.ID = i.ID

IF @.ID <> 0

BEGIN

-- FIND OUT NO OF GTS FOR SPECIFIC DSTS TO UPDATE FOR CORRESPONDING GT
DECLARE GTList CURSOR FOR
-- SELECT GTS FOR DSTID
SELECT GTs.GTID
FROM GTs INNER JOIN
GTDSTs ON GTs.GTID = GTDSTs.GTID INNER JOIN
ITSPs ON GTs.ID = ITSPs.ID
Where GTs.ID = @.ID AND DSTID = @.DSTID

OPEN GTList

FETCH NEXT FROM GTList INTO @.GTID

-- IF NO GT FOUND OF SPECIFIC DST FOR ITSP THEN REJECT
IF (@.GTID = 0)
BEGIN
SELECT @.intErrorCode = 1
CLOSE GTList
DEALLOCATE GTList
GOTO PROBLEM

END
-- Check @.@.FETCH_STATUS to see if there are any more rows to fetch.
WHILE @.@.FETCH_STATUS = 0

BEGIN

-- CHECK WIEHTER GT ENTERY EXISTS IN TR TABLE
-- IF SO THEN UPDATE ELSE INTER NEW FOR GT

SELECT @.GTExist = GTid
FROM TR
WHERE GTid = @.GTid AND DSTID = @.DSTID

IF @.GTExist > 0

UPDATE TR
SET RT = @.RT,
Time = getdate()

WHERE GTID = @.GTid


ELSE

INSERT INTO TR (GTID, DSTID, RT)
VALUES (@.GTID,@.DSTID,@.RT)
-- IN CASE FOR ANY EXCEPTION GO TO PROBLEM PARA AND CLOSE & DEALLOCATE CURSOR
SELECT @.intErrorCode = @.@.ERROR
IF (@.intErrorCode <> 0)
BEGIN
CLOSE GTList
DEALLOCATE GTList
GOTO PROBLEM
END
FETCH NEXT FROM GTList INTO @.GTID


END
CLOSE GTList
DEALLOCATE GTList
COMMIT TRAN
END
ELSE
-- INVALID ID
BEGIN
RAISERROR (' Invalid ID ', 16, 1)
ROLLBACK TRAN
END


PROBLEM:
IF (@.intErrorCode <> 0)
BEGIN
IF (@.intErrorCode = 1)
RAISERROR (' Insert is rejected due to invalid GT DST info', 16, 1)
ELSE
RAISERROR ('Error occured to udated info for tr' s. Please contact administrator ', @.intErrorCode, 1)
ROLLBACK TRAN


END|||Hi aldo_2003

I am Waiting.. Only problem is that if i use multiple insert with GO then multiple insert works.. but when i call is in cursor for multiple then it add only first insert..|||Just got back from lunch mate

Am goiung to try a couple if things and get back to you

Cheers|||Had a quick look at the trigger

Don't think it is the GO statement as I managed to get a test trigger to fire twice without the GO.

I'm not sure why your trigger is only firing once.

What you might want to do is to try and reduce the complexity of the code within the trigger by putting that logic within the stored proc and calling this from your trigger.

You have cursors within cursors when you take the trigger into account and this may be causing you issues that you are not aware of.

Get the trigger to fire properly without any logic in it i.e

Get the first cursor inserting into your table and the trigger firing a simple insert into a test table. Once you have that then implement your trigger logic in a stored proc and call that from the trigger.

I hope this helps, let me know how you get on.

Cheers|||Well there are just so many things...

first you don't need a cursor...collapse the cursor and the insert in to 1...

second a cursor in a trigger can't be a good idea performance wise...but like I said collapse them

third

-- IF NO GT FOUND OF SPECIFIC DST FOR ITSP THEN REJECT
IF (@.GTID = 0)

isn't a check for existance...

Look at @.@.ROWCOUNT

fourth...never mind...fixe the insert first...

Friday, March 23, 2012

Multiple Foreign Keys

I am having trouble creating multiple foreign keys on a table so that I can set up cascading update and cascading delete from two different primary tables. I am using the diagram to do this but when I try to save the diagram I get the following error.

ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_IndResults_RaceData'. The conflict occurred in database 'VIRA', table 'RaceData', column 'RaceID'.

What would cause this to happen? Is it possible that I have records in the foreign table that do not transfer back to the primary table?

Thanks!Is it possible that I have records in the foreign table that do not transfer back to the primary table?Not just possible, nearly guaranteed.

The best answer is to create a query that will show you the offending rows, then go and fix them. You can often pick a value that makes a reasonable default (sometimes NULL works well, meaning there isn't any relationship). Sometimes you've got to figure out what FK value you need, which can be a lot of work.

-PatP|||Thanks! That was exactly what it was and it is fixed!

Friday, March 9, 2012

Multiple connections in a CLR trigger

Hello

Is there nay way that it is possible to connect to another database within a trigger…

My scenario is that an update in a table in database X triggers several updates in a table in Database Y. The databases are on the same SQL Server

Thanks for any ideas, solutions or hints you can give me..!!!

Hi

I developed one solution like that Try Referencing server Y in server X wthi sp_addlinkedserver and then directly try to make the update like this

update servery.database.dbo.tabley set yfield=xfield

from serverx.database.dbo.tablex

|||

I tried the suggested function but i didn’t help and I still didn’t figure out how to make it work

Cheers

|||Did you try to just instatiate a new sqlconnection object using a normal connection string ? Make sure that you will need to grant a special trust level (external) to the assembly to make it work.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

I did try to instantiate a new sqlconnection object. But I sadly I don't know about the trust level which you are talking about... Could you give me further information have I could grant the trust level to the assembly? It would be highly appreciated

Thanks

|||This would be a quick sample:

// Create Table Table1

//(

// SomeInt INT

//)

//INSERT INTO Table1 VALUES (1)

//EXEC sp_configure 'clr enabled' , '1'

//ALTER DATABASE Northwind SET TRUSTWORTHY ON

//RECONFIGURE

// Enter existing table or view for the target and uncomment the attribute line

[Microsoft.SqlServer.Server.SqlTrigger(Name = "Trigger1", Target = "Table1", Event = "FOR INSERT")]

public static void Trigger1()

{

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = "Select 1";

conn.Open();

cmd.ExecuteNonQuery();

}

Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Hi,
I think the “CLR Triggers for SQL Server 2005” article on
http://aspalliance.com/1273_CLR_Triggers_for_SQL_Server_2005.all
may be helpful in this discussion.

This popular white paper is written by a software engineer from our organization Mindfire Solutions (http://www.mindfiresolutions.com).

I hope you find it useful!

Cheers,
Byapti

Wednesday, March 7, 2012

Multiple column update statement

Hi,

I'm new to SQL Server but not new to SQL because I used it with Oracle. I wonder if it is possible to do this kind of statement on SQL Server:

UPDATE TableX M
SET (M.column1, M.column2, M.column3)=
(SELECT T.column1, T.column2, G.column3)
FROM Table_Y T,
Table_Z G
WHERE join condition))
WHERE EXISTS (join condition for TableX)

Basically, I'd like to update multiple columns in one statement but for some reason I can not get it to work.

What would be the equivalent on SQL Server?

Thanks for the help,

Laszlo M

SQL Server does not support row value constructors yet. So you will have to use the TSQL UPDATE statement extension which has a FROM clause like:

UPDATE TableX

SET column1 = T.column1, column2 = T.column2, column3 = G.column3

FROM TableX as M, Table_Y as T, Table_Z as G

WHERE <join condition>

and <condition from exists>

See Books Online UPDATE statement topic for more examples and details on the syntax.

|||

Hi Laszlo,

UPDATE TableX
SET column1 = T.column1, column2 = T.column2, column3 = G.column3
FROM TableX M INNER JOIN
(TableY T INNER JOIN TableZ G ON T.column4 = G.column1)
ON M.column2 = G.column1

Regards,

Mike H.

|||

Thanks for the quick response. All of you were very helpful.

Laszlo

Saturday, February 25, 2012

Multiple Batches via ADO

Hi,
how would I go about sending a Update Script (with multiples Batches) via
ADO to an SQL 2000 Server.
As much as I know the Command.CommandText can only take a single query.
Thanks a lot
PatrickHi Patrick
You can try using UpdateBatch option available in ADO
thanks and regards
Chandra
"Patrick Wolf" wrote:

> Hi,
> how would I go about sending a Update Script (with multiples Batches) via
> ADO to an SQL 2000 Server.
> As much as I know the Command.CommandText can only take a single query.
> Thanks a lot
> Patrick
>
>|||Hi Chandra,
thanks but this is to batch update a recordset. Eg. if you make multiple
updates in a recordset it enables you to post them all at once.
It doesnt allow you to pass in a script.
Thanks
Patrick
"Chandra" <Chandra@.discussions.microsoft.com> wrote in message
news:F00BB8E6-3D0F-46E1-9111-95F68AE3CF88@.microsoft.com...
> Hi Patrick
> You can try using UpdateBatch option available in ADO
> thanks and regards
> Chandra
>
> "Patrick Wolf" wrote:
>|||Hi Patrick
You can try executing the different scripts in a loop or u can send multiple
queries saperated by a semi colon.
Command.CommandText "query1;query2;query3"
thanks and regards
Chandra
"Patrick Wolf" wrote:

> Hi Chandra,
> thanks but this is to batch update a recordset. Eg. if you make multiple
> updates in a recordset it enables you to post them all at once.
> It doesnt allow you to pass in a script.
> Thanks
> Patrick
> "Chandra" <Chandra@.discussions.microsoft.com> wrote in message
> news:F00BB8E6-3D0F-46E1-9111-95F68AE3CF88@.microsoft.com...
>
>|||The simplest way to accomplish this is to build a large string with all of t
he
statements you wish to execute and execute the statement. Anything you write
in
QA, you can execute this way.
Thomas
"Patrick Wolf" <ppjwolf@.bigfoot.com> wrote in message
news:On4RWZsTFHA.3184@.TK2MSFTNGP15.phx.gbl...
> Hi,
> how would I go about sending a Update Script (with multiples Batches) via
ADO
> to an SQL 2000 Server.
> As much as I know the Command.CommandText can only take a single query.
> Thanks a lot
> Patrick
>|||Hi Thomas,
that is the answer I was looking for thanks.
I did't know that this would be possible.
I will try it out.
Good wishes
Patrick
"Thomas Coleman" <thomas@.newsgroup.nospam> wrote in message
news:OLKhkLyTFHA.2768@.tk2msftngp13.phx.gbl...
> The simplest way to accomplish this is to build a large string with all of
> the statements you wish to execute and execute the statement. Anything you
> write in QA, you can execute this way.
>
> Thomas
>
> "Patrick Wolf" <ppjwolf@.bigfoot.com> wrote in message
> news:On4RWZsTFHA.3184@.TK2MSFTNGP15.phx.gbl...
>