Showing posts with label columns. Show all posts
Showing posts with label columns. Show all posts

Thursday, March 29, 2012

duplicated rows

Hello,

I have a table T1 with fields ID, F1, F2, F3, F4, F5, F6….

I need to find if there is duplicated rows based on F1, F2, F3 columns. If there is set F5='minimum' where ID is MIN(ID). So the smallest should be set as minimum. How can I do this in a stored procedure?

Hi JIM.H:

FYI, here is a quick sampleBig Smile:

create table tbl_testDup (ID int primary key, F1 varchar(50), F2 varchar(50), F3 varchar(50),
F4 varchar(50), F5 varchar(50), F6 varchar(50))


create procedure sp_MarkMin
as
update tbl_testDup set F5='minimum'
where ID in
(select min(ID) from tbl_testDup group by F1,F2,F3)
go

duplicated rows

Hello,

I have a table T1 with fields ID, F1, F2, F3, F4, F5, F6….

I need to find if there is duplicated rows based on F1, F2, F3 columns. If there is set F5=’minimum’ where ID is MIN(ID). So the smallest should be set as minimum. How can I do this in a stored procedure?

You can do something like below (assuming that ID column is unique):

update T1

set F5 = 'minimum'

where ID = (

select min(t.ID)

from T1

group by F1, F2, F3

having count(*) > 1

)

Else you can do below:

update t1

set F5 = 'minimum'

from T1 as t1

join (

select F1, F2, F3, min(t.ID) as min_id

from T1

group by F1, F2, F3

having count(*) > 1

) as t2

on t2.F1 = t1.F1 and t2.F2 = t1.F2 and t2.F3 = = t1.F3

where t2.min_id = t1.ID

|||Thanks, this helps a lot. It seems I need to mark all other rows as
NotMin. Is there a way to mark all the other duplicated rows as SET f5 =
'NotMinimum' if they are not Min(ID), there mgth be more than two rows
duplicated.|||

You can change the 2nd UPDATE stmt to:

update t1

set F5 = 'NotMinimum'

from T1 as t1

join (

select F1, F2, F3, min(t.ID) as min_id

from T1

group by F1, F2, F3

having count(*) > 1

) as t2

on t2.F1 = t1.F1 and t2.F2 = t1.F2 and t2.F3 = = t1.F3

where t2.min_id < t1.ID

Duplicate values

I have a table with 6 columns. I want to create a primary key for the first
two columns but I am getting a message that I already have duplicate values
in the combination of the two column.
Can someone help me with SQL to find the duplicate rows on two columns of
the same table ?
Thanks.SELECT T1.col1, T1.col2, T1.col3, T1.col4, T1.col5, T1.col6
FROM YourTable AS T1,
(SELECT col1, col2
FROM YourTable
GROUP BY col1, col2
HAVING COUNT(*)>1) AS T2
WHERE T1.col1 = T2.col2
AND T1.col1 = T2.col2
(untested)
David Portas
SQL Server MVP
--|||CORRECTION:
...
WHERE T1.col1 = T2.col1
AND T1.col2 = T2.col2
David Portas
SQL Server MVP
--|||Thanks......I found it........Now, I need to find a way to remove the
duplicates (Leave only one row).
Thanks.
"David Portas" wrote:

> CORRECTION:
> ...
> WHERE T1.col1 = T2.col1
> AND T1.col2 = T2.col2
>
> --
> David Portas
> SQL Server MVP
> --
>
>

Duplicate values

I have a table with 6 columns. I want to create a primary key for the first
two columns but I am getting a message that I already have duplicate values
in the combination of the two column.
Can someone help me with SQL to find the duplicate rows on two columns of
the same table ?
Thanks.
SELECT T1.col1, T1.col2, T1.col3, T1.col4, T1.col5, T1.col6
FROM YourTable AS T1,
(SELECT col1, col2
FROM YourTable
GROUP BY col1, col2
HAVING COUNT(*)>1) AS T2
WHERE T1.col1 = T2.col2
AND T1.col1 = T2.col2
(untested)
David Portas
SQL Server MVP
|||CORRECTION:
...
WHERE T1.col1 = T2.col1
AND T1.col2 = T2.col2
David Portas
SQL Server MVP
|||Thanks......I found it........Now, I need to find a way to remove the
duplicates (Leave only one row).
Thanks.
"David Portas" wrote:

> CORRECTION:
> ...
> WHERE T1.col1 = T2.col1
> AND T1.col2 = T2.col2
>
> --
> David Portas
> SQL Server MVP
> --
>
>

Duplicate values

I have a table with 6 columns. I want to create a primary key for the first
two columns but I am getting a message that I already have duplicate values
in the combination of the two column.
Can someone help me with SQL to find the duplicate rows on two columns of
the same table ?
Thanks.SELECT T1.col1, T1.col2, T1.col3, T1.col4, T1.col5, T1.col6
FROM YourTable AS T1,
(SELECT col1, col2
FROM YourTable
GROUP BY col1, col2
HAVING COUNT(*)>1) AS T2
WHERE T1.col1 = T2.col2
AND T1.col1 = T2.col2
(untested)
--
David Portas
SQL Server MVP
--|||CORRECTION:
...
WHERE T1.col1 = T2.col1
AND T1.col2 = T2.col2
David Portas
SQL Server MVP
--|||Thanks......I found it........Now, I need to find a way to remove the
duplicates (Leave only one row).
Thanks.
"David Portas" wrote:
> CORRECTION:
> ...
> WHERE T1.col1 = T2.col1
> AND T1.col2 = T2.col2
>
> --
> David Portas
> SQL Server MVP
> --
>
>

Duplicate table structures to a new database

Hi all expert,

Question as stated title above, how do I duplicate the table structure to another database whether it copies all columns, primary and foreign keys, default values, descriptions, constraints, indexes

Many thanks for the help.

regards,

Use Enterprise Manager's Generate Script Feature, and execute the Generated SQL on your target server.

|||

you can also check Database Publishing Wizard.

http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en

Madhu

|||

I want to done this using T-SQL. So what is the script it can be?

|||

There isn't a built-in t-sql command to script However, you can use sp_oa* and smo/dmo. You can check google for archive script.

As to just creating a new table based on the old one, you can just do this:

select top 0 *

into new_table

from old_table

Note that this method does not create constraints/dependencies on the new table.

|||

You can run a trace with profiler to see what Management Studio does to create the script, is a bit more than i expected, but if you really want to you should be able to replicate that on your own.

|||Manivannan gave you the answer. Use the Generate Script feature, it will make the script for you, and then you can re-use that script.

|||

Yes I know there is a Generate Script functions. But I want to schedule it and run on every month, therefore a pre-generated script must be prepared so that it can auto run on every month.

So, can this be done? Thanks a lot for the help.

regards

sql

Duplicate table

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
Thanks
To duplicate the table columns and data, just:
SELECT * INTO Newtable FROM Oldtable
However this will not copy indexes, access privileges, etc.
To get everything you must script the table to create it, and then add the
data, perhaps with
INSERT INTO NewTable SELECT * FROM Oldtable
Of course, using column names instead of * would be a better practice.
Rick Byham (MSFT)
This posting is provided "AS IS" with no warranties, and confers no rights.
"Omar Abid" <omar.abid2006@.gmail.com> wrote in message
news:1181578789.815330.212000@.q69g2000hsb.googlegr oups.com...
> Hi,
> In my program i need to duplicate a table in a current data base.
> I'm thinkin' of reading the data base columns and then rows and so i
> create another table
> Is there any other easy and fast method with SQL Server 2005, because
> my idea is so slow
> I'm using VB 2005 Express with SQL Server 2005 Express
> Thanks
>
|||> INSERT INTO NewTable SELECT * FROM Oldtable
> Of course, using column names instead of * would be a better practice.
And might be necessary, e.g. for IDENTITY/ROWVERSION...
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006

Duplicate table

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
ThanksHi
On Jun 11, 5:19 pm, Omar Abid <omar.abid2...@.gmail.com> wrote:
> Hi,
> In my program i need to duplicate a table in a current data base.
> I'm thinkin' of reading the data base columns and then rows and so i
> create another table
> Is there any other easy and fast method with SQL Server 2005, because
> my idea is so slow
> I'm using VB 2005 Express with SQL Server 2005 Express
> Thanks
If you just want to create copy the data then you could use
SELECT *
INTO #temptable
FROM Mytable
Which will create a new table with the contents of the old table, but
the new table will not have the constraints such as primary or foreign
keys and indexes that the original table had. If you dont want the
table but just the columns use a where clause that will never be
satisfied such as
WHERE 1 = 0
Alternatively you can use SMO see Allen Whites posts in the thread
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=876099&SiteID=1
John

duplicate table

Hi,

I have a table with data in it, (tblT1)

A. How do I create a table tblT2 with the same columns in tblT1, but without data in (blank tblT2 table with columns).

B. And also how do I copy the enitre table's data (tblT1) into tblT2.

Thank you,

Give a look in books online to SELECT ... INTO.

Here are some previous posts:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=126194&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=307375&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=691090&SiteID=1

|||

Got it . I will check it out.

Thank you.

Duplicate table

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
Thanks
SELECT * INTO NewTable FROM OldTable
Andrew J. Kelly SQL MVP
"Omar Abid" <omar.abid2006@.gmail.com> wrote in message
news:1181578846.356369.101240@.n4g2000hsb.googlegro ups.com...
> Hi,
> In my program i need to duplicate a table in a current data base.
> I'm thinkin' of reading the data base columns and then rows and so i
> create another table
> Is there any other easy and fast method with SQL Server 2005, because
> my idea is so slow
> I'm using VB 2005 Express with SQL Server 2005 Express
> Thanks
>

Duplicate table

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
Thanks
Hi
On Jun 11, 5:19 pm, Omar Abid <omar.abid2...@.gmail.com> wrote:
> Hi,
> In my program i need to duplicate a table in a current data base.
> I'm thinkin' of reading the data base columns and then rows and so i
> create another table
> Is there any other easy and fast method with SQL Server 2005, because
> my idea is so slow
> I'm using VB 2005 Express with SQL Server 2005 Express
> Thanks
If you just want to create copy the data then you could use
SELECT *
INTO #temptable
FROM Mytable
Which will create a new table with the contents of the old table, but
the new table will not have the constraints such as primary or foreign
keys and indexes that the original table had. If you dont want the
table but just the columns use a where clause that will never be
satisfied such as
WHERE 1 = 0
Alternatively you can use SMO see Allen Whites posts in the thread
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=876099&SiteID=1
John
sql

Duplicate table

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
ThanksOmar Abid <omar.abid2006@.gmail.comwrote in news:1181578579.728121.204320
@.g4g2000hsf.googlegroups.com:

Quote:

Originally Posted by

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
Thanks
>
>


SELECT * INTO NewTable FROM OldTable|||On 11 juin, 18:54, "Chris.Cheney" <Chris.CheneyXXNOSPA...@.tesco.net>
wrote:

Quote:

Originally Posted by

Omar Abid <omar.abid2...@.gmail.comwrote in news:1181578579.728121.204320
@.g4g2000hsf.googlegroups.com:
>

Quote:

Originally Posted by

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
Thanks


>
SELECT * INTO NewTable FROM OldTable


Hi
Thx for your reply I'll try your code and submit the result to you
Omar Abid

Duplicate table

Hi,
In my program i need to duplicate a table in a current data base.
I'm thinkin' of reading the data base columns and then rows and so i
create another table
Is there any other easy and fast method with SQL Server 2005, because
my idea is so slow
I'm using VB 2005 Express with SQL Server 2005 Express
ThanksHi
On Jun 11, 5:19 pm, Omar Abid <omar.abid2...@.gmail.com> wrote:
> Hi,
> In my program i need to duplicate a table in a current data base.
> I'm thinkin' of reading the data base columns and then rows and so i
> create another table
> Is there any other easy and fast method with SQL Server 2005, because
> my idea is so slow
> I'm using VB 2005 Express with SQL Server 2005 Express
> Thanks
If you just want to create copy the data then you could use
SELECT *
INTO #temptable
FROM Mytable
Which will create a new table with the contents of the old table, but
the new table will not have the constraints such as primary or foreign
keys and indexes that the original table had. If you dont want the
table but just the columns use a where clause that will never be
satisfied such as
WHERE 1 = 0
Alternatively you can use SMO see Allen Whites posts in the thread
http://forums.microsoft.com/MSDN/Sh...876099&SiteID=1
John

Tuesday, March 27, 2012

Duplicate results in 2 columns but reversed

I have to write a query which extracts everyone from a table who has the same surname and forenames as someone else but different id's.

The query should have a surname column, a forenames column, and two id columns (from the person column of the table).

I need to avoid duplicates i.e. the first table id should only be returned in the first id column and not in the second - which is what i am getting at the mo.

This is what i have done

select first.surname, first.forenames, first.person, second.person
from shared.people first, shared.people second
where first.surname= second.surname
and first.forenames = second.forenames
and not first.person = second.person
order by first.surname, first.forenames

and i get results like this

Porter Sarah Victoria 9518823 9869770
Porter Sarah Victoria 9869770 9518823 - i.e. duplicates

cheerswhat about:

select
first.surname,
first.forenames,
min(first.person),
min(second.person)
from
shared.people first, shared.people second
where
first.surname= second.surname
and first.forenames = second.forenames
and not first.person = second.person
group by
first.surname,
first.forenames
order by
first.surname,
first.forenames

I'm not positive that this would work if there were more than one duplicate entry.

regards,

hmscott|||Thanks for the quick reply,

Yes, it seems to work if the first column is called min(first.person) and the second is called max(second.person) but this would fail if there were more than one duplicate.

Any ideas?

Cheers :)|||This will be a pig, so I hope you only need to run this once...

select
first.surname,
first.forenames,
min(first.person),
min(second.person)
from
shared.people first, shared.people second
where
first.surname= second.surname
and first.forenames = second.forenames
and first.person < second.person
group by
first.surname,
first.forenames
order by
first.surname,
first.forenames|||I hope you are talking about something like this:

drop table #tmp
create table #tmp(id int,fname varchar(10),lname varchar(10))
insert #tmp values(1,'a','b')
insert #tmp values(2,'b','b')
insert #tmp values(3,'a','b')
insert #tmp values(4,'f','b')
insert #tmp values(5,'b','b')
insert #tmp values(6,'b','b')
insert #tmp values(7,'a','b')

select distinct t.fname,t.lname,t.id,t2.id
from #tmp t
join #tmp t2 on t2.fname=t.fname and t2.lname=t.lname and t2.id<>t.id
where t.fname+t.lname in(
select fname+lname
from #tmp
group by fname+lname
having count(*)>1)
order by 1,2,3,4

--- OR

select fname,lname,id
from #tmp
where fname+lname in(
select fname+lname
from #tmp
group by fname+lname
having count(*)>1 )
order by 1,2,3|||cheers all - thanks for the quick responses!

:D

Duplicate records

how to we check in for duplicate records without using sort (remove duplicateS)

i need to remove duplicates based on four columns.

please let me know

Are the duplicates coming from a SQL query?

The sort transformation is really the best option, in my opinion, unless you can issue a "SELECT DISTINCT" in your source pull.|||

source is a text file.Is it possible to use sort as i am deciding distinct based on four columns.

For each row coming i will be looking at four columns to decide if the records are distinct.

|||

sureshv wrote:

source is a text file.Is it possible to use sort as i am deciding distinct based on four columns.

For each row coming i will be looking at four columns to decide if the records are distinct.

Yes, pick your four columns and sort by them. Then click the remove duplicates. It will remove duplicates based on the chosen sort columns.

However, do you need to pick one row over another, or can you discard any row without knowing which row you are keeping?|||You can do this with a synchronous non-blocking script component pretty easily, especially if the data is already sorted by the rows you want to check.

The following code concatenates all your columns that you want to test for uniqueness into one string "key". If the key of the current row is different from the key of the previous row, then you redirect it to the output. Otherwise it gets dropped. This is so fast and simple that you should try to sort your data as it comes from the source in the order of these columns.

Code Snippet

Dim PreviousKey As String = ""

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
Dim ThisKey As String = Row.FirstName + "_" + Row.LastName + "_" + Row.LastName + "_" + Row.EmailAddress
If ThisKey <> PreviousKey Then
Row.DirectRowToOutput0()
PreviousKey = ThisKey
End If
End Sub


If the data cannot be sorted, then instead of comparing ThisKey to PreviousKey, you would check for the existence of the current key in a hash table. If it doesn't exist, you redirect the row and add it. This will get slow if you have lots of unique values and you have to have enough memory to hold all that data.

|||Yet another option is to stage your data file into a sql server table and then select distinct from there.|||

JayH wrote:

You can do this with a synchronous non-blocking script component pretty easily, especially if the data is already sorted by the rows you want to check.

Nevermind. I was too slow with my post. If the source is a text file, then you should just use the sort component.
|||

If i use sort it does remove duplicates.can i stored the removed duplicates in a table .Is it possible?

|||

sureshv wrote:

If i use sort it does remove duplicates.can i stored the removed duplicates in a table .Is it possible?

The sort REMOVES duplicates. You can't redirect them. Instead, if you want to do what you've just stated, use the sort transformation but leave the remove duplicates box UNCHECKED. Then use JayH's code below to allow you to check for duplicates and then to redirect rows that are in "duplicate."|||

sureshv wrote:

If i use sort it does remove duplicates.can i stored the removed duplicates in a table .Is it possible?

Nop. That is the bad thing about the Sort transform technique. You don't have control over which rows get discarded. I always stage data in tables; then I can use sql to handle that. In SQl Server 2005 and oracle for example, I use the RANK() function to detect and enumerate the duplicates. Then in data flows I use a conditional split to let pass all myRank=1 and redirect the rest to an error table is that is required.

If you don't use staging tables; you still can acomplish it using a script component.

|||

I have one problem. I use sort component and it tries to get all data from the source. i connected my sort output to script as u said...But i dont find rows comming out of the sort output and stays yellow at sort.it dosent go to next step.What does it mean i unchecked remove duplicates..and i select 4 columns which i want and sort order for them is 1,2,3,4...is it wrong with text file or have i not set it right.

I really appreciate all help from u guys..Thank u very much...

|||

sureshv wrote:

I have one problem. I use sort component and it tries to get all data from the source. i connected my sort output to script as u said...But i dont find rows comming out of the sort output and stays yellow at sort.it dosent go to next step.What does it mean i unchecked remove duplicates..and i select 4 columns which i want and sort order for them is 1,2,3,4...is it wrong with text file or have i not set it right.

I really appreciate all help from u guys..Thank u very much...

The sort is a resource intensive transformation. It requires to 'get' all rows prior to sort/dedup them, so you won't see any row in the output untill all rows have been consumed by the sort . If the data set is big and/or memory available for SSIS is small, the package can perform poorly.

|||

the incoming data seem to have 2,190,234 rows.I know that sort will wait for all incoming rows and then sort it.So what do i need to do for its performance to increase...Is their any efficient method to run faster..

Please le me know

Thanks again

|||

sureshv wrote:

the incoming data seem to have 2,190,234 rows.I know that sort will wait for all incoming rows and then sort it.So what do i need to do for its performance to increase...Is their any efficient method to run faster..

Please le me know

Thanks again

Stage the data first in SQL server. It is another option that I gave you earlier today. Then SELECT from the source using an ORDER BY clause choosing your four columns. Then use Jay's approach to determine if the incoming record is in duplicate or not.|||

Phil Brammer wrote:

sureshv wrote:

the incoming data seem to have 2,190,234 rows.I know that sort will wait for all incoming rows and then sort it.So what do i need to do for its performance to increase...Is their any efficient method to run faster..

Please le me know

Thanks again

Stage the data first in SQL server. It is another option that I gave you earlier today. Then SELECT from the source using an ORDER BY clause choosing your four columns. Then use Jay's approach to determine if the incoming record is in duplicate or not.

If you are going to stage the data in SQL Server 2005 table; I would use the t-sql rank() function and a conditional split in the dataflow to deduplicate and redirect the non-desire rows; as I explained in my other post bellow.

Thursday, March 22, 2012

duplicate dhcek on two columns

Hello, I have a table T1 and on this table I have an insert trigger. In the trigger I need to check if T1.ID and T1.Type=’xyz’ together are duplicated or not (duplicate dhcek on two columns), if yes return error from trigger. There might be T1.ID and T1.Type=’abc’ duplicated, that is fine.

Inside the trigger do something like:

IF EXISTS (SELECT * FROM Table T INNER JOIN INSERTED I ON T.Id = I.Id AND T.Type = I.Type)
RAISERROR('Upps',15,10)

If you do not want to use a trigger you can also use a native UNIQUE constraint which will keep track of the UNIQUE-ness.

HTH, Jens K. Suessmeyer.


http://www.sqlserver2005.de

|||

Hi Jens,

Thanks for your reply. Uniqueness would not work since if the type is not xyz it can be duplicated. I had to change my strategy a little bit. Why wouldn’t the following code work?

This is part of my trigger on table T1. I am trying to check if the records inserted to T1 is available in myDB.dbo.myTable or not (destination table). If it is available rollback T1. It does not do that although I insert the same records twice.

-- duplicate record check

SET @.step = 'Duplicate record'

IF EXISTS (

SELECT i.myID, i.Type

FROM INSERTED i INNER JOIN

myDB.dbo.myTable c ON i.myID = c.myID

GROUP BY i.myID, i.Type

HAVING (COUNT(*) > 1) AND (i.Type = 'In')

)

BEGIN

ROLLBACK transaction

RAISERROR('Error: step: %s. rollback is done.', 16, 1, @.step)

Return

END

What is problem?

|||First of all the trigger will be only rolled back if already two instances of the record exists in the database and you want to insert a third one(I guess sthat this is not what you want right).

-- duplicate record check

SET @.step = 'Duplicate record'

IF EXISTS (

SELECT *

FROM INSERTED i INNER JOIN

myDB.dbo.myTable c ON i.myID = c.myID --You don′t need to use the three part name until the table is located on another database

WHERE i.Type = 'In' --Don′t you want to check if the existing data in the table is also of type c.type = 'In' ?

)

BEGIN

ROLLBACK transaction

RAISERROR('Error: step: %s. rollback is done.', 16, 1, @.step)

Return

END

Do you really want to check another database for the value ? Or did you just do this for full declaration of the database reference ?

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

Hi Jens,

Thanks you very much for your help. Here is a little bit more detail. The trigger is on T1. T1 is inserted records by an external system. T1.Type can be “In’, or ‘NotIn’ If it is In I need to insert this to a remote database too which is myDB.dbo.myTable. There is no myTable.Type field in remote database.

1. So if T1.myID=1 and T1.Type=’In’ comes more than one and myID=1 is already available in remote database return error to the external system and do not accept insert into T1.

2. If T1.myID=1 and T1.Type=’NotIn’ can come more than one continue without error.

How should I do this?

|||

We are getting closer :-)

IF EXISTS
(
SELECT * FROM T1
INNER JOIN INSERTED I
ON T1.myId = I.MyId
AND T1.Type = I.Type
WHERE T1.Type = 'In'
INNER JOIN mydb.dbo.MyTable MT
ON MT.MyId = T.MyId
)
RAISE THE ERROR

In my word: The query sorts out, if already a myId record with the Type = 'In' exists (which is currently trying to insert into the table) and check if that is the case if there is another myId in the remote database. (Thats because of your "and" in "...than one AND myId=1...").

If there is already an entry in the local table but no in the remote table there won′t be an error presented (as of your explanation) If this is not the right behaviour you either explained it in a wrong way or I did understand it the wrong way :-)

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Thanks Jens. I will try this.

duplicate dhcek on two columns

Hello,

I have a table T1 and on this table I have an insert trigger. In the trigger I need to check if T1.ID and T1.Type='xyz' together are duplicated or not (duplicate dhcek on two columns), if yes return error from trigger. There might be T1.ID and T1.Type='abc' duplicated, that is fine.

Hi

I don't think you can return error from trigger directly to your application.

You can set a flag to indicate error happened.

 
CREATE TRIGGER trgAfterInsertOn T1FOR INSERTASBEGIN declare @.idint declare @.typevarchar(32)SELECT @.id=id,@.type=typeFROM INSERTEDIFNOT EXISTS(SELECT *FROM T1WHERE T1.id=@.id )begin if (@.type='xyz')begin/* insert error flag */end endEND

Wednesday, March 21, 2012

Dumping rows of 2 columns of a table to a different table with different no of columns

Hi,

I have two tables

Table Source

{

category varchar(20),

LastUpdate datetime

}

Table Destination

{

SubjectId varchar(20),

SubjectDate datetime,

category varchar(20),

LastUpdate datetime

}

Please note that the number columns are different in each table.

I wanted to dump the data of Source table to Destination table. I meant to say that the rows of 2 columns in Source table to last 2 rows of Destination table.

And also my oreder of the columns in Destination table will vary. So i need to a way to dynamically insert the data in bulk. but i will know the column names for sure before inserting.

Is there anyway to bulk insert into these columns.

Your quick response will be appreciated

~Mohan Babu

It is a very simple solution:

1. create a view on destination
CREATE VIEW [viewDestination]
AS
SELECT category, LastUpdate
FROM dbo.Destination

2. Then
insert into dbo.viewDestination
select * from Source

doesn't matter the order in Destination table.|||Thanks for the reply.But i don't want to create a view on this as the number of coulmns on the table will be changed dynamically. Can you please suggest any other way

Dumping rows of 2 columns of a table to a different table with different no of columns

Hi,

I have two tables

Table Source

{

category varchar(20),

LastUpdate datetime

}

Table Destination

{

SubjectId varchar(20),

SubjectDate datetime,

category varchar(20),

LastUpdate datetime

}

Please note that the number columns are different in each table.

I wanted to dump the data of Source table to Destination table. I meant to say that the rows of 2 columns in Source table to last 2 rows of Destination table.

And also my oreder of the columns in Destination table will vary. So i need to a way to dynamically insert the data in bulk. but i will know the column names for sure before inserting.

Is there anyway to bulk insert into these columns.

Your quick response will be appreciated

~Mohan Babu

It is a very simple solution:

1. create a view on destination
CREATE VIEW [viewDestination]
AS
SELECT category, LastUpdate
FROM dbo.Destination

2. Then
insert into dbo.viewDestination
select * from Source

doesn't matter the order in Destination table.|||Thanks for the reply.But i don't want to create a view on this as the number of coulmns on the table will be changed dynamically. Can you please suggest any other way

Wednesday, March 7, 2012

dtsx iterate rows of table

In a dtsx , is it possible to use a For Loop or ForEach loop to iterate
through the rows of a table , extract the values of certain columns , and
conditionally call a sproc ( that takes as parameters the values of some of
the columns ) ?
How to do this ? Or is a dtsx loop not the recommended way to perform this
task.On Mar 13, 6:33 pm, "John Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
> In a dtsx , is it possible to use a For Loop or ForEach loop to iterate
> through the rows of a table , extract the values of certain columns , and
> conditionally call a sproc ( that takes as parameters the values of some of
> the columns ) ?
> How to do this ? Or is a dtsx loop not the recommended way to perform this
> task.
You may want to repost on the DTS/SSIS message groups. They are:
microsoft.public.sqlserver.dts and
microsoft.public.sqlserver.integrationsvcs. Sorry I could not be of
greater assistance.
Regards,
Enrique Martinez
Sr. SQL Server Developer