Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Unique?

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-07-11 : 13:23:21
Help please.

I need to run the following to bring data from one table to another:



insert into MNI.dbo.MNI (
MNINO
,ECSOID
,[LNAME]
,[FNAME]
,[MNAME]
,[Uniquekey])

Select 'NOLINK'+Cast(WarrantID as varchar(6)) as MNINO
,'NOLINK'+CAST (WarrantID as varchar(6)) as ECSOID
,Substring(Lastname,1,20) as Lname
,Substring(Firstname,1,15) as fname
,Substring(MIddlename,1,15) as Mname
,SCADMIN.dbo.fnsc_GetUniqueID(newid())

FROM douglas.dbo.WarrantVictim

GO


But here's the kick. In this particular table, there cannot be duplicate MNInos or ECSOID values. So i really only need it to insert records with unique WarrantID values. Is there a way to modify this script to do that?

thanks

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-07-11 : 13:50:34
If I understand you correctly, add this:
WHERE WarrantID IN(SELECT WarrantID 
FROM douglas.dbo.WarrantVictim
GROUP BY WarrantID
HAVING COUNT(*)=1)
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-07-11 : 13:56:58
Rob,

perfect

thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-07-11 : 14:01:06
Here's one way:
INSERT
....
SELECT
MNINO
,ECSOID
,Lname
,fname
,Mname
,UnNamedColumn
FROM
(
SELECT
'NOLINK'+Cast(WarrantID as varchar(6)) as MNINO
,'NOLINK'+CAST (WarrantID as varchar(6)) as ECSOID
,Substring(Lastname,1,20) as Lname
,Substring(Firstname,1,15) as fname
,Substring(MIddlename,1,15) as Mname
,SCADMIN.dbo.fnsc_GetUniqueID(newid()) AS UnNamedColumn
,ROW_NUMBER() OVER (PARTITION BY WarrantID ORDER BY WarrantID) AS RowNum
FROM
douglas.dbo.WarrantVictim
) AS T
WHERE
RowNum = 1
Go to Top of Page
   

- Advertisement -