SQL insert query help needed

Hi all

I am new to SQL and i would to know if anyone can help me work around this
problem:

i have 2 tables

table1 (column1, column2, column3, column4) where column1 = integer - auto
increment, clumn2 = text, column3 = text , column 4 = integer

and

table2 (column1 , column2) where column1 = integer - auto increment and
column2 = text

Now table1.column4 is related to table2.column1 thats table1.column4 = table2.
column1

what i was trying is to be have an SQL statement that can insert values that
are not fixed maybe different every time
but i want them to be added into table1 and given for example ('Tom', 'Jones',
'Music')
where 'Tom' corresponds to tale1.column2 , 'Jones' corresponds to table1.
column3 but 'Music' correspond to table2.column2 and i want to add the
corresponding integer of that description that is the table2.column1 

I have tried the following although i know its wrong but maybe it helps

select table1.column2,table1.column3,table2.column2 from table1, table2
(INSERT INTO table1 (column2,column3, column4) VALUES ('Tom', 'Jones',
'Music'))
where table1.column4=table2.column1

1. Excuse my english
2. Thanx in advance

0
Pantso
6/9/2010 4:27:35 PM
sqlserver.programming 1873 articles. 0 followers. Follow

2 Replies
1069 Views

Similar Articles

[PageSpeed] 7

I am not sure I understand the requirements, but try the following:

INSERT INTO table1 (column2, column3, column4) 
SELECT T1.column2, T1.column3, T2.column2 
FROM table1 AS T1
JOIN table2 AS T2
  ON T1.column4 = T2.column1;

-- 
Plamen Ratchev
http://www.SQLStudio.com
0
Plamen
6/9/2010 6:53:46 PM
Seems like you have to insert data into the parent table first, so that you 
could get the reference ID.  Then you can get the inserted ID by the built-in 
function SCOPE_IDENTITY()

DECLARE @Table2ID int;

INSERT INTO Table2 (column2) VALUES ('Music');
SET @Table2ID = SCOPE_IDENTITY();

INSERT INTO Table1 (column2, column3, column4)
VALUES ('Tom', 'Jones', @Table2ID);

SELECT * FROM Table2;
SELECT * FROM Table1;


"Pantso" wrote:

> Hi all
> 
> I am new to SQL and i would to know if anyone can help me work around this
> problem:
> 
> i have 2 tables
> 
> table1 (column1, column2, column3, column4) where column1 = integer - auto
> increment, clumn2 = text, column3 = text , column 4 = integer
> 
> and
> 
> table2 (column1 , column2) where column1 = integer - auto increment and
> column2 = text
> 
> Now table1.column4 is related to table2.column1 thats table1.column4 = table2.
> column1
> 
> what i was trying is to be have an SQL statement that can insert values that
> are not fixed maybe different every time
> but i want them to be added into table1 and given for example ('Tom', 'Jones',
> 'Music')
> where 'Tom' corresponds to tale1.column2 , 'Jones' corresponds to table1.
> column3 but 'Music' correspond to table2.column2 and i want to add the
> corresponding integer of that description that is the table2.column1 
> 
> I have tried the following although i know its wrong but maybe it helps
> 
> select table1.column2,table1.column3,table2.column2 from table1, table2
> (INSERT INTO table1 (column2,column3, column4) VALUES ('Tom', 'Jones',
> 'Music'))
> where table1.column4=table2.column1
> 
> 1. Excuse my english
> 2. Thanx in advance
> 
> .
> 
0
Utf
6/11/2010 1:58:18 AM
Reply:

Similar Artilces:

Gantt Chart in Excel. Do I need conditional formatting?
Hello all. I have created a Gantt chart that shows approx. 15 tasks in each project. I am using this to show percentage of completion for each task. It is working just fine and looks great but I need it to insert a date when a task is at 100% completion to show the client the date that particular task was completed. How would I do this without ruining my chart I already have working fine? I know this is probably easy to do but I cannot seem to keep the rest of the chart intact. Is conditional formatting the answer? Thanks! You could use Rob Bovey's XY Chart Labeler to do this: h...

timeclock report help
We use the timeclock report to print out total hours worked for any given two-week pay period. I am trying to modify the timeclock report by adding an additional column that will tell me whether the date on the report was in week 1 of the pay period, or week 2. I would like to return the string 'Week1' if the TimeIn value for that row is less than or equal to the FilterLoLim value + 7, but this does not work, i can't refer to this in my SQL formula. Any ideas?? Thank you, Kevin here is a simple select statement to demonstrate what i'm trying to accomplish: select cas...

how do I insert sequential numbering on a sheet of 10 pages?
-- Morrell http://www.publishermvps.com/Default.aspx?tabid=95 -- Ed Bennett - MVP Microsoft Publisher ...

Xpath Newbie Mistake, Please Help!
This code throws an exception when it should just print "C$" to the screen. What is messed up in my xpath? Thanks. test.cs: using System; using System.Xml; using System.IO; class XmlReadSimple { [STAThread] static void Main(string[] args) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load( Path.GetDirectoryName(System.Reflection.Assembly. GetExecutingAssembly().Location) + "\\test.xml" ); System.Xml.XmlNode xmlNode = xmlDocument.SelectSingleNode( "///[@path='c:\']/@name" ); Console.WriteLine(...

Help on displaying recession periods in a financial/ stock chart
Using excel version (10.6501.6626)sp3 How do you shade the chart background to show economic recession periods in a financial/ stock chart. I've fiddled with the drawing function-rectangle box, transparency, etc- but it isn't very satisfactory since it corrupts the column/ line colors and usually washes out when printed. So that doesn't appear to be the solution. And after checking thru Microsoft help and the discussion group messages I haven't found a message addressing my problem. Since we see charts all the time with the recession periods shown I know there is a way to acco...

Can I insert multiple scales on the Y axis of a single XY graph?
I am trying to plot three parameters on a single XY line chart. Is it possible io have three different scales charted to cover all the three parameters which are quite variable numerically? Using Excel 2000 No, you can only have a left and a right y-axis. Workaround: let's assume one series has very small numbers; have a column with these values multiplied by say 1000; plot this column and use a legend like "voltage x 1000" If one series is too large, have a column with the numbers divided; legend "Sales in $million) best wishes -- Bernard V Liengme www.stfx.ca/people...

Need help removing characters in multiple cells
I have an excel 2007 sheet that has over 300 cells in a column that have a name of a store plus a number between paranethesis. Is there a one step way I can remove the paranethesis? Here is an example: K-Mart (ABC#73846365) and I want to have just the following K-Mart Each cell has a different store name and set of numbers within the paranethesis. Thanks Select the range to fix. Edit|replace what: _(* (spacebar, open paren, asterisk) with: (leave blank) replace all It looked like you'd want to remove the space character after the t in k-mart. "<----- Mardm...

Help Understanding Microsoft Server Products
Hi all We are looking to upgrade servers (currently running WinServer 2003) - new hardware and software. The Essential Business Server Premium pack appears to be ideal in terms of the software and package offered. BUT it appears that there will not be a version of this supporting Exchange 2010 for some months. This means we put a significant investment into 3 year old software, rather than catching the up-to-date flavour. So what's the difference between buying the EBS package and buying individual software applications separately? Also, is there additional software i...

File in 2007 form
I was trying the Beta Office 2007 and made the mistake of saving one of you Excel file in the new 2007 form. I really need it back in the 2003 form. Problem: due to a variety of issues, I de-installed the Beta version and went back to 2003 but for got about the one file.. So; are there any utilities that anyone knows of the will convert this file back two 2003 form?? It is a simple Excel file, meaning that there are no clever or fancy formula or macros, just a collection of parts and part numbers gathered over MUCH time surfing various web sites. Any help or advise (beside the obvious....

SQL Server 2005 CAL question
If I have 1 HQ Server with with 2 HQ Clients, how many CALS do I need at the HQ server. Thanks in advance. Phil get a min of 5 cals Its not only the clients to think about its also any office PC that connects to SQL. "Phil V" <PhilV@discussions.microsoft.com> wrote in message news:3E2F3590-032F-4B4C-9CEC-5A6A6BC7F46D@microsoft.com... > If I have 1 HQ Server with with 2 HQ Clients, how many CALS do I need at > the > HQ server. Thanks in advance. > > Phil ...

Mail Security
I am running Exchange 5.5 on Windows advanced server2K. From my Exchange server, logged in as Administrator, you can do the following: start->settings->control panel->mail....then change the mailbox user to anyone in the company......open MS Outlook and read their email... Is there a way I can prevent this from happening (even if only for my own mailbox) Thank you! If Administrator has the role of Service Account Admin on the site object, then this can happen. You wouldn't be the first person who used this account as the service account. You could change the service ac...

help with range lookup and date criteria
Hello, I have a list of doctors (column A) that are each on-cal through a number of days. My argument is: if the date value of B1 an C1 is within the current date, then repeat the value of A1 -- James Spaldin ----------------------------------------------------------------------- James Spalding's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=2567 View this thread: http://www.excelforum.com/showthread.php?threadid=39088 hi, James ! > ... a list of doctors (column A) that are each on-call through a number of days. > ... argument is: if the date value ...

Max Query
Hi all, I have 2 tables. One holds account numbers. One holds invoices, linked to account numbers. Ive got a query to show the latest invoice for each account number, via the MAX function. However, once this has been done, due to it being an aggregate function. There is no way of me editing this query once done. Is there any way around this, as i would like to only show the latest invoice, and edit information in that. Regards, A correlated sub-query in the WHERE clause might work for you if you don't have a lot of records. SELECT * FROM Accounts INNER JOIN Invoic...

Help with complex query 05-09-07
Hi all, I've got a query that I'm not sure how to develop. My tables: Quotes - QuoteNo, RaisedBy, Customer QuoteItems - RecordID, QuoteNo, PartNo, Lifecycle, Value There's a one-to-many relationship between Quotes and QuoteItems, i.e. one quote can have many items. I need to run a query to show a list of quotes with totals from the QuoteItems table i.e. QuoteNo, RaisedBy, Customer, List of PartNos, List of Lifecycles, TotalValue I haven't got a clue how to start this, I know it needs to be nested queries, but the listing of parts and lifecycles is particularly stumpin...

Help with Date/Time Function
I am working with date/time functions trying to figure out how many hours were spent doing a job. I have a start time and a finish time. But I need to subtract out any time between five pm and six am and weekends from the times. These would be times that no one was actually working on the job. For example I have 4/5/2005 18:23 4/6/2005 17:04 Could someone give me a clue as to how this might be done? Thanks, Steve Monczka smonczka@hotmail.com It's not a trivial thing, here's an example http://www.cpearson.com/excel/DateTimeWS.htm or here http://tinyurl.com/cbphn -- R...

I need installation software.
I write my software in Visual C++ version 6. It has been many years since I used InstallShield and I don't think I have that software anymore. Right now I need a fairly simple installation utility program. Is there any good FREE (or low cost) installation software that I can download? TonyG wrote: > I write my software in Visual C++ version 6. It has been many years > since I used InstallShield and I don't think I have that software > anymore. Right now I need a fairly simple installation utility program. > > Is there any good FREE (or low cost) installation soft...

Problem with a Array formula, Please help !!
What is the problem with the array formula (see cells in column R, which are highlighted coloured yellow) ? {=IF(SUM(IF(ISERROR(LOOKUP(N(OFFSET($B2,,{0,2,4,6,8,10,12,14})),IF($A2="ABC",A!$A$2:$G$140,B!$A$2:$G$210))),0,IF(LOOKUP(N(OFFSET($B2,,{0,2,4,6,8,10,12,14})),IF($A2="ABC",A!$A$2:$G$140,B!$A$2:$G$210))="Y",1,0)),IF(ISERROR(LOOKUP(T(OFFSET($B2,,{0,2,4,6,8,10,12,14})),IF($A2="ABC",A!$A$2:$G$140,B!$A$2:$G$210))),0,IF(LOOKUP(T(OFFSET($B2,,{0,2,4,6,8,10,12,14})),IF($A2="ABC",A!$A$2:$G$140,B!$A$2:$G$210))="Y",1,0))) >0,"Y...

Money 2002 opens very slowly! Started few days ago!!! HELP
Hi to all, I have problem with Money2002. It is full with data from last year and until few days ago everything was ok. In average it would took about 3-5sec for MsMoney to open all the data, but since yesterday I need to wait app 10- 15sec for MsMoney to open. I tried everything but no luck. Anybody have a clue what this could be. Thanks in advance Is 15 seconds really that long to wait? What other programs and data have you installes/saved in the last year. Is your drives getting full? In microsoft.public.money, John wrote: > >I have problem with Money2002. It is full with...

the new excel help is horrible!!!! (way to go, limiting comments)
how do I get the previoulsy found HELP to list the typical formulas, or use of special characters, like the & symble in excel. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane. http://www.microsoft.com/office/community/en-us/default.mspx?mid=102753c7-0f12-402b-9da0-3...

help with CRM1.2 deployment
Hi, I installed MS CRM1.2 on a Windows 2003 server. The sample database (Adventure Cycles Work) and license key provided used; CRM server added to the Pre-Windows 2000 compatible access group; installed and accessed as domain administrator. When I access the web application at the server with localhost or server name, it works just fine. However, when I try to access it through client machines in the same domain, IE will simplely crash/disappear after I input my user name and password. Someone please help me... Thanks in advance! -duoduo It is due to the fact that MBS CRM uses pop-up wi...

OnNewWindow2 message help.
Hi there, I have the following code: void CMFCIEEvtSpyDlg::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel) { // create new instance of iespy LPDISPATCH tmpDisp; CMFCIEEvtSpyDlg* dlgPop = new CMFCIEEvtSpyDlg; // create form dlgPop->Create(IDD_MFCIEEVTSPY_DIALOG); // register as browser dlgPop->m_webBrowser.SetRegisterAsBrowser(true); tmpDisp = dlgPop->m_webBrowser.GetApplication(); dlgPop->m_webBrowser.UpdateWindow(); *ppDisp = tmpDisp; // dump data to the window. AddEventToList(WBListBox, "NewWindow2"); } which is kinda edited from the MSDN example of c...

Help with monitoring software
Hello, We have a several exchange 2003 servers running on windows 2003. I was asked to find a monitoring software that will allow us to monitor exchange server (services, uptime, connitivity, logs,database,...) and windows 2003 in very comprehensive way for under $5K. Can any one recommend a good product? By the way, we have MOM but it's handled by a different group so we need something else. Thanks On Sat, 2 Dec 2006 07:02:00 -0800, desert-mania <desertmania@discussions.microsoft.com> wrote: >Hello, > >We have a several exchange 2003 servers running on windows 2003...

SQL options and new upgrade GP9.0 (from GP7.5)
Here is my problem after I upgraded to GP9.0. I need to add a user with GP, unless I refresh its password (in SQL Enterprise Manager) and re-define its properties, I cannot use it to access GP moreover, use SQL resources. I also discoverred that DYNGRP dont have permissions on some of the GP tables. Also when I change the password in GP. It does not update the one used in SQL. Why is that? How can I correct this issue? If you create a user in GP the password is encrytped by GP so that the password that is sent to SQL is not the same. This is why a GP user can not do anything in SQL...

unable to sign you in to Windows Live ID .. Please HELP
I get the following error when I try to access my email from the Messanger icon (Messanger knowes the number of unread emails). I have been experiencing the problem for several months and have been searching for a solution but have not been able to find one. I am able to login to my Windows Live Email via the web. I have deleted my account and reinstalled it and I have coppied all my email to a backup folder neither of these have worked. Unable to send or receive messages for the Live (Kellyr02) account. Sorry, we were unable to sign you in to Windows Live ID at this time. ...

does \exchsrvr\address need to be shared?
Hi All, we are running exchange 2k3. THis came up as a potential risk during a security assessment. Does this folder need to be shared? I could not find anything on the subject. Thanks! On Sun, 11 Apr 2010 09:04:01 -0700, lenny <lenny@discussions.microsoft.com> wrote: >we are running exchange 2k3. THis came up as a potential risk during a >security assessment. Does this folder need to be shared? Do you have more than one Exchange server? If the anser is yes, the the answer to your question is also yes. >I could not find >anything on the subject. ...