Click here to Skip to main content
15,898,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
oddball question, but i'd like to insert a csv into my database. one of the columns in the table references a foreign key. i was wondering if it were possible to change the value of the the column upon insert to reflect the value of the foreign key.

sorry it sounds convoluted, but here's an example...

table: players----------------------------------- table: country
playerid | playerName | countryID ------------- countryID | countryName

i'd like to insert into the players table, but rather than countryID...the csv i have has country names instead. is it possible to change something like this...

george, england

to

george, 1

upon insert?

thank you and sorry for the goofy question/phrase
Posted
Updated 14-Aug-13 21:07pm
v2
Comments
RedDk 15-Aug-13 12:58pm    
Not two tables ...

Ok, then I don't have any idea what [nvarchar] to [int] might provide as advantage after conversion. Inform me.

1 solution

Two tables here?

Table01:
CREATE TABLE [tbl_MX_players](
	[playerId][int], 
		[playerName][nvarchar](129), 
			[countryID][int]
	)

Table02:
CREATE TABLE [tbl_MX_country](
	[countryID][int],
		[countryName][nvarchar](45)
	)

Anyway. Save those/that .csv "table(s)" as TAB DELIMITED .txt for use in BULK INSERTION.
INSERT INTO  [tbl_MX_players]
	FROM 'c:\users\mx\table01_tdt.txt'

INSERT INTO [tbl_MX_country]
	FROM 'c:\users\mx\table02_tdt.txt'

Since you've got a potentially common data point here, [countryID], that could be used in a JOIN statement to bring the two tables together into one output during a SELECT. See the BOL for details and sample examples of how to JOIN in TSQL.

As for substitution of "address" with "int" ... not sure what you mean to do there.

But to start, toss the .csv for .txt and do the CREATE tABLE. Once data's in the database, CREATE TABLE once more usg [Idx][int]IDENTITY(1,1) to add an index.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900