Link to home
Start Free TrialLog in
Avatar of sklarbodds
sklarbodds

asked on

Import table data into stored proc

I have a table that contains some contact data I need to import into my database.

Basically I need to know how to loop through the table and send each record to a stored proc that adds users like below (I know my syntax is way off).  I don't know if I should use a while loop or what, but I need to have the table values for parameters in the stored proc.

SELECT FirstName, LastName, etc FROM dbo.Import
INTO usp_AddContact FirstName, LastName, etc
Avatar of JestersGrind
JestersGrind
Flag of United States of America image

You can do something like this:

Greg



DECLARE @Counter INT, 
		  @FirstName VARCHAR(50), 
		  @LastName VARCHAR(50), 
		  etc
 
SET @Counter = 1
 
SELECT ROW_NUMBER() OVER (ORDER BY FirstName, LastName, etc) AS Row, FirstName, LastName, etc 
INTO #Import
FROM dbo.Import
 
WHILE @Counter <= (SELECT MAX(Row) FROM #Import)
 
BEGIN
	
	SELECT @FirstName = FirstName, @LastName = LastName, etc
	FROM #Import
	WHERE Row = @Counter
	
	EXEC usp_AddContact @FirstName, @LastName, etc
	
	SET @Counter = @Counter + 1
	
END

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of expertsoul
expertsoul
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sklarbodds
sklarbodds

ASKER

Thanks to both of you, I ended up using the cursor because it seemed the most straightforward.