SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Finding Records
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Steve2106
Posting Yak Master

United Kingdom
166 Posts

Posted - 07/04/2013 :  09:18:39  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

Romania
403 Posts

Posted - 07/04/2013 :  09:40:14  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 07/05/2013 :  02:12:19  Show Profile  Reply with Quote
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

Edited by - visakh16 on 07/05/2013 02:12:49
Go to Top of Page

Steve2106
Posting Yak Master

United Kingdom
166 Posts

Posted - 07/05/2013 :  03:08:01  Show Profile  Reply with Quote
Hi There,

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

Best Regards,

Steve.

Edited by - Steve2106 on 07/05/2013 03:08:24
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000