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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Finding Records

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2013-07-04 : 09:18:39
Hi There,

In need of your help again.

A while ago I was asked to import some records from a .csv file into one of our data tables.
I am now being asked to supply the id for each record imported.
The only way I can think of doing this to run through the .csv file getting the value in the description column and finding the matching description in my sql table.

I do not know how to write the sql to achieve this.
Could someone kindly help me.

Thanks.

Best Regards,


Steve

stepson
Aged Yak Warrior

545 Posts

Posted - 2013-07-04 : 09:40:14
Hi,

depending on how you do the import

if you import into a "stage" tbl and then move/copy the records to your main Tbl
then you can use output clause.Read more about this

http://msdn.microsoft.com/en-us/library/ms174335.aspx

some sample

DECLARE @MyTableVar table(
LastName nvarchar(20) NOT NULL,
FirstName nvarchar(20) NOT NULL,
CurrentSales money NOT NULL
);

INSERT INTO dbo.EmployeeSales (LastName, FirstName, CurrentSales)
OUTPUT INSERTED.LastName,
INSERTED.FirstName,
INSERTED.CurrentSales
INTO @MyTableVar
SELECT c.LastName, c.FirstName, sp.SalesYTD
FROM Sales.SalesPerson AS sp
INNER JOIN Person.Person AS c
ON sp.BusinessEntityID = c.BusinessEntityID
WHERE sp.BusinessEntityID LIKE '2%'
ORDER BY c.LastName, c.FirstName;

SELECT LastName, FirstName, CurrentSales
FROM @MyTableVar;
GO




S

Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mut
sabinWeb
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-05 : 02:12:19
The only way I can think of doing this to run through the .csv file getting the value in the description column and finding the matching description in my sql table
Not a foolproof method IMHO. Unless you've no other columns for validating, you should never rely on description. Reason is there can be adhoc modifications done to description field and so current values may not be same as original ones which you exported from csv. In case you're sure the system cant have any other changes, then you're safe. Otherwise I would suggest using some other more reliable column (or group of columns) for identification like say primary key column, audit column like date created etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2013-07-05 : 03:08:01
Hi There,

Thanks for your advice I have managed to do this now.

Best Regards,

Steve.
Go to Top of Page
   

- Advertisement -