Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT and INSERT in same sql statement?

Status
Not open for further replies.

vangundy

Programmer
Jan 29, 2005
38
CA
How can I write the following statement?

PSEUDOCODE:
Look in the database and see if this book was reserved between Date1 and Date2. If the book was not reserved between Date1 and Date2 then INSERT the variables (Book Name, Author and Company) into that table. Is it possible to add SELECT and INSERT into the same statement?

Help is greatly appreciated!

Johnny
 
Provided you are using SQL Server, this is a question better suited for the SQL Server forum (forum183). If not, you might want to find the DB application you're using and refer it from there.

As an afterthought, though, I believe it should be possible to combine the two statements (granting that I am anything but a SQL expert). Using a rough outline which you will need to modify, you can use this as a sample:
Code:
INSERT INTO myTable (BookName, Author, Company) VALUES (SELECT "The Cat in the Hat", "Dr. Seuss", "Random House" FROM myTable WHERE Resrv_Dt IS NOT BETWEEN '4/1/2005' AND '4/7/2005')
There may be others who can offer additional assistance in the aforementioned forums.

------------------------------------------------------------------------------------------------------------------------
"I am not young enough to know everything."
Oscar Wilde (1854-1900)
 
Assuming you are populating existing database fields and that you are updating existing records rather than INSERTing new ones, you would need to use UPDATE. Something like:

Code:
UPDATE myTable SET BookName='The Cat in the Hat' WHERE Resrv_Dt IS NOT BETWEEN '4/1/2005' AND '4/7/2005')
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top