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
 General SQL Server Forums
 New to SQL Server Programming
 Inserting multple records in one shot?

Author  Topic 

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 15:10:11
Hi,
I need to insert multiple records in history table & I've list of codes to be stored in these table:

1234, 5678, 5467, 7685

All above code exists in tableA but do not exists in tableB. How can I insert them in one query shot (without using procedure, etc)?

Please note that while inserting in tableB, I need to insert the date column field same as the date column field from tableA for that code.

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 15:12:28
Do you mean like this:

INSERT INTO TableB(...)
SELECT ... FROM TableA a
WHERE NOT EXISTS (SELECT * FROM TableB b WHERE a.ID = b.ID)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 15:16:47
Hi Tara!
Thanks for the quick reply. I need to insert only certain codes in to tableB, say 1234, 5678, 5467, 7685
These codes are input which needs to be inserted in tableB. They are already present in tableA.

How can I do this insertion?
Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 15:22:41
If you know the code and want one query, then do this:

INSERT INTO TableB
SELECT Column1, Column2
FROM TableA
WHERE Column1 = 1234
UNION ALL
SELECT Column1, Column2
FROM TableA
WHERE Column1 = 5678
UNION ALL
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 15:26:46
Hi Tara!
Thanks again for quick reply! Unfortunately, I need to update 500 codes. But I came up with following query:

insert into tableB (...., date,...)
select ..., date,... from tableA where code in ('1234', '4567', ......)

This is working. At any given time, I will not have more than 500 codes to be inserted. Will performance be an issue if I use it as shown above?

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 15:27:48
Yes performance could be an issue. You will need to test.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 15:28:29
If you are able to use WHERE IN, then why not make the query more intelligent so that you don't need to specify each code?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 15:30:53
Actually, I'm creating the variable which contains all comma separated codes which will not be more than 500 at any given time & then using that variable dynamically in the IN clause.

Are you saying that it could still cause issue?

Note: the codes are input from client (customer)

Thanks!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-21 : 15:32:46
then you're going to need this

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=150444

Where are you getting the codes from in the first place?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

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





Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 15:33:20
Yes that is a performance concern.

Do not use dynamic SQL for this. Search the forums and the internet for various ways to handle a dynamic IN.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 15:35:39
Hi,
I'm getting the codes from the customer through body parameters. I'm implementing this sql in the web service. And the web service gets the code through the body parameter.

As I mentioned earlier, I do not want to use procedure or function. Would the query using IN clause work for me?

Thanks!
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 15:38:19
Ok. I've one more question. Before this insert, I need to update one column called status in tableA for each code & then need to perform the insert.

Is there a way to combine both update & inserts in one shot? I'll probably consider using proc or func if you can tell me how & whats the best way to do this in one shot! Please note that at any given time, I won't have more than 500 codes.

Thanks!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-21 : 15:43:24
No

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

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





Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-21 : 15:44:46
wait...I take that back

Define..."in one shot"

and look at the link I posted

and WHY can't you use a function

And ok, you get the codes via a web service...but WHERE do you store them?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

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





Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 15:51:56
Hi,
The codes are already present in tableA. I just need to update the status for inputted codes & then insert them into tableB.

I looked at your earlier reply. So, should I use the below function? I'm a kind of newbie to functions!

CREATE FUNCTION dbo.udf_Table(@ParmList varchar(8000), @Delim varchar(20))
RETURNS @table TABLE
(Parameter varchar(255))
AS

BEGIN
DECLARE @x int, @Parameter varchar(255)

WHILE CHARINDEX(@Delim, @ParmList)-1 > 0
BEGIN
INSERT INTO @table(Parameter) SELECT SUBSTRING(@ParmList,1,CHARINDEX(@Delim, @ParmList)-1)
SELECT @ParmList = SUBSTRING(@ParmList,CHARINDEX(@Delim, @ParmList)+1, LEN(@ParmList)-CHARINDEX(@Delim,@ParmList))
END
INSERT INTO @table(Parameter) SELECT @ParmList
RETURN
END
GO

Why above function inserts 2 times: one in the loop & one outside loop? Also, how can I modify it so that it'll perform updates too?

Thanks!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-21 : 15:55:40
Well...you said you were going to build a delimited list

You would use the function to read the list

If the values are already in a table, you shoyuld just join to it and be done with it

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

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





Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-21 : 15:57:42
post the ddl of your tables, sample data and what you expect the results to be

It will be far easier for us to understand



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

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





Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-09-21 : 16:01:01
Yes, I do have comma separated list of codes as input. But I'm pretty new to SQL & functions.

I need to update the status in tableA for all these codes & then insert them in tableB. Would you be able to post the modified function?

Thanks again for answering all my queries!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-21 : 16:08:43
BEGIN TRAN

UPDATE a
SET STATUS = '?'
FROM TableA a
JOIN dbo.Table(@Codes,',')
ON a.code = parameter

-- perform error handling

INSERT INTO Tableb(Code)
SELECT Parameter FROM dbo.Table(@Codes,',')

-- perform error handling

COMMIT TRAN

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

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





Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-09-21 : 16:39:29
>> I need to insert multiple records [sic] in history table <<

Row are not records.

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Please learn to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you want to learn how to ask a question on a Newsgroup, look at: http://www.catb.org/~esr/faqs/smart-questions.html

>> I've list of codes to be stored in these table: 1234, 5678, 5467, 7685 <<

Do you know the SDQL idioms for Hitory tables? What you posted has no (start_date, end_date) pairs to place it in a temporal space.

>> All above code exists in TableA but do not exists in tableB. How can I insert them in one query shot (without using procedure, etc)? <<

Unh? Who cares if they are in TableA, if you want them in TableB, just use this:

INSERT INTO TableB (unknown_column) -- no DDL, no column name!
VALUES (1234), (5678), (5467), (7685);

>> Please note that while inserting in TableB, I need to insert the date column field [sic] same as the date column field [sic] from tableA for that code <<

Please use real terminology; tables have columns, not fields. The use of "field" in SQL means a part of a temporal data type that loses meaning by itself (i.e. year, month, day, hour, minute, second).

Next, why do you want redundant data in your schema? Most of us try to get rid of it. Based on wild guessing, did you mean something like this:

INSERT INTO TableB (unknown_column, vague_date) -- no DDL, no column name!
SELECT unknown_column, vague_date
FROM TableA
WHERE unknown_column
IN (1234, 5678, 5467, 7685);

Please read the FAQ about posting. Then read a good RDBMS and how to remove redundant data in a schema with Normalization.



--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -