|
-
Jan 31st, 2006, 03:05 PM
#1
Thread Starter
Lively Member
SQL Stored Procedure Question/Looping Records
I have Table A with 3 cloumns x,y,z Varchars.(data is a first middle lastname stuff)
I have Table b that is supposed to Store the Concatenated result of (xyz)
I have a stored procedure that is supposed to move through Table A and read the 3 columns then insert the result to table b. It works for 1 record only then stops, I am unsure how to get it to do more than one record.
Code:
CREATE PROCEDURE namecat @newname nvarchar(50)
AS
select @newname = (X + Y + Z) from TableA
Insert into TableB (xyz) Values (@newname)
GO
Sorry for the Newish question but im thiking a Cursor is needed?
-
Jan 31st, 2006, 03:43 PM
#2
Re: SQL Stored Procedure Question/Looping Records
Cursors should be a last resort and in this case they are not needed.
Insert Into TableB (xyz) Select x + ', ' + y + ' ' + z From TableA
What is the purpose of the @NewName parameter?
-
Jan 31st, 2006, 04:49 PM
#3
Thread Starter
Lively Member
Re: SQL Stored Procedure Question/Looping Records
The new name parameter was how i was going to pass the cont cat variables to the insert statment.
-
Jan 31st, 2006, 05:26 PM
#4
Re: SQL Stored Procedure Question/Looping Records
 Originally Posted by smilbuta
The new name parameter was how i was going to pass the cont cat variables to the insert statment.
You are thinking like a programmer - and T-SQL needs you to lose that old iterative/logic processing mindset.
You need to try to see how an entire "grid" of data can be prepared - as in the SELECT statement that BRUCEVDE showed you - and that grid will effortlessly pass into an INSERT statement.
All in one shot - that's the beauty of set-based logic and SQL...
-
Jan 31st, 2006, 07:26 PM
#5
Re: SQL Stored Procedure Question/Looping Records
Code:
insert into b values
b.xyz = a.xyz
from
(select x + y + z xyz
from a) a
edit:
first idea
basically you use a subquery for it but i have not tested it wait...
-
Jan 31st, 2006, 07:34 PM
#6
Re: SQL Stored Procedure Question/Looping Records
oceane...
BRUCEVDE's syntax is fine - INSERT ... SELECT ... will work in MS SQL server. Probably in ACCESS as well.
I do not believe that you need to so carefully wrap it in ()'s to indicate it's sub-query nature or that you are required to alias it.
-
Jan 31st, 2006, 07:38 PM
#7
Re: SQL Stored Procedure Question/Looping Records
hehhe yep there's a bug in it .. i tested this new one 
Code:
DECLARE @a TABLE(x char(1), y char(2), z char(3))
DECLARE @b TABLE(xyz varchar(5))
insert into @a values('a', 'b', 'c')
insert into @a values('d', 'e', 'f')
select x + y + z xyz from
@a
-- this is the only statement needed
insert into @b
select x + y + z xyz from
@a
his problem was because he only gets the first record, this one gets all in one statement.. though not sure in access. but here is the t-sql
-
Jan 31st, 2006, 07:39 PM
#8
Re: SQL Stored Procedure Question/Looping Records
LOLz did not see that one sorry bruce
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|