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 rows into existing SQL table

Author  Topic 

New2SQL2
Starting Member

6 Posts

Posted - 2008-05-21 : 17:00:17
Hi all,

I am trying to insert some new rows into an existing SQL table. The table name is Agt_table, and I want to add some data for some new agents into existing columns:
Agent name, agent code, phone number, fax number

Example -
I want to add the following record to my existing table Agt_table
Agent name: ABC Company
Agent code: 012345
Phone #: 555-555-5555
Fax#: 555-555-5555

How would I write the sql statement to do that?

Thanks so much!!!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-21 : 17:01:48
INSERT INTO TableName (...)
VALUES (...)

Check SQL Server Books Online for details.

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

New2SQL2
Starting Member

6 Posts

Posted - 2008-05-21 : 17:04:05
I am SUPER new to SQL - can you show me exactly what I'd write for the example above?

I really appreciate it! Thanks!
Go to Top of Page

New2SQL2
Starting Member

6 Posts

Posted - 2008-05-21 : 17:13:12
Can anyone else help me?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-21 : 17:20:48
Wouldn't you learn more if you looked it up or googled it?

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

New2SQL2
Starting Member

6 Posts

Posted - 2008-05-21 : 17:24:58
I have been looking for an hour and a half and can't seem to find anything. I literally just started using SQL yesterday, and a lot of what I read is way over my head. Believe me, I am trying to learn this! :) I am the only one in my office that is learning SQL, so I'm on my own.

Any help would be greatly appreciated. Thanks so much.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-21 : 17:27:19
Here is an example:

INSERT INTO Table1 (Column1, Column2)
VALUES (1, 'Tara')

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

New2SQL2
Starting Member

6 Posts

Posted - 2008-05-21 : 17:31:59
So would this be right???

INSERT INTO Agt_Table (Agent_Name, Agent_Code, Phone_, Fax_)
VALUES ('ABC Company', '012345', '555-555-5555','555-555-5555')
VALUES ('DEF Company', '456789', '555-123-4567, '555-123-4566')
etc.

Thank you so very much.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-21 : 17:35:19
Not for MICROSOFT SQL Server.

INSERT INTO Agt_Table (Agent_Name, Agent_Code, Phone_, Fax_)
VALUES ('ABC Company', '012345', '555-555-5555','555-555-5555')

INSERT INTO Agt_Table (Agent_Name, Agent_Code, Phone_, Fax_)
VALUES ('DEF Company', '456789', '555-123-4567, '555-123-4566')



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-21 : 17:35:24
For multiple rows, you can run INSERT INTO/VALUES multiple times per row or do this:

INSERT INTO Table1 (Column1, Column2)
SELECT 1, 'Tara' UNION ALL
SELECT 2, 'Mike' UNION ALL
SELECT 3, 'Andy'

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

New2SQL2
Starting Member

6 Posts

Posted - 2008-05-21 : 17:39:49
I think I got it! Thanks so much Peso and Tara!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-22 : 01:43:21
quote:
Originally posted by Peso

Not for MICROSOFT SQL Server.

INSERT INTO Agt_Table (Agent_Name, Agent_Code, Phone_, Fax_)
VALUES ('ABC Company', '012345', '555-555-5555','555-555-5555')

INSERT INTO Agt_Table (Agent_Name, Agent_Code, Phone_, Fax_)
VALUES ('DEF Company', '456789', '555-123-4567, '555-123-4566')



E 12°55'05.25"
N 56°04'39.16"



I heard this is possible in SQL 2008

INSERT INTO Agt_Table (Agent_Name, Agent_Code, Phone_, Fax_)
VALUES ('ABC Company', '012345', '555-555-5555','555-555-5555'),
('DEF Company', '456789', '555-123-4567, '555-123-4566')
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-22 : 02:48:39
Yes it is. The difference in syntax is the comma between VALUES part.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -