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
1018 Views

Similar Articles

[PageSpeed] 1

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:

Receipt Printer Help!!
Can a Verifone 250 credit card receipt printer be used with RMS as a cash receipt printer? If so, what cable connectors would be needed to do this. The only cable connector we have goes to the credit card terminal. Kelly Why would you want to? Very slow printer!!!! Get a Samsung erp-350 or a 270. Probibly have to have a custom cable. >-----Original Message----- >Can a Verifone 250 credit card receipt printer be used >with RMS as a cash receipt printer? If so, what cable >connectors would be needed to do this. The only cable >connector we have goes to the credit card ...

Suspicion of CListCtrl memory leak, Help !
I have a testing MFC DialogBase project which has only one control -- ListCtrl. When the app exit , i found that the memory is not free by the system by watch the System Performance dialog page. All that i do is within CListCDlg::OnTimer() func, show as bellow: < code snippet> void CListCDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CListCDlg) DDX_Control(pDX, IDC_LIST1, m_MyList); //}}AFX_DATA_MAP } void CListCDlg::OnTimer(UINT nIDEvent) { TCHAR sContent[ 128 ]; //I have not alloc any data from heap, all the data added to CListCtl is ...

like query using field name
Hi All I have a column 'staffnumber' (int) and another column 'Resourceid' (nvarchar) in a table in sql server 2005 express I want to do a query to find rows where the staffnumber value is not in the Resourceid field e.g select mydate, customerid from appointments where Resourceid not like '%[staffnumber value]%' in other words the 'staffnumber' column value for each row is NOT in the 'Resourceid' column value Can this be done? Regards Steve Steve declare @par nvarchar(20) set @par='staffnumber' select my...

Calling Dex Procedures from SQL
Is there any way to call dex procs from SQL or VB code? Jed There is an undocumented and unsupported method of calling Dex code from VBA. However, I know of no method of calling Dexterity from SQL. Please email me if you want an example, just remove the online. from the address below. David Musgrave [MSFT] Senior Development Consultant MBS Services - Asia Pacific Microsoft Business Solutions http://www.microsoft.com/BusinessSolutions mailto:dmusgrav@online.microsoft.com Any views contained within are my personal views and not necessarily Microsoft Business Solutions policy. This p...

Need with with 4125 RWW Not Working
SBS2008 Remote from Win 7 Pro and XP Pro clients I am having an issue with getting RWW to work in the SBS 2008 environment. Basically I can connect to the server via \remote from a off-site location but when I try and "Connect to a Computer" I get the following message. "The wizard cannot configure Remote Desktop Connection settings. Make sure that the client version of the Remote Desktop Protocol (RDP) 6.0 or later is installed on this computer." This also includes when I try and connect to the SBS 2008 server itself. At first I thought when this didn...

VLOOKUP help. Im an Excel Newbie!!1
I have an Invoice worksheet which I must use VLOOKUP function to import data such as customer name, customer code, customer address, etc. My study sheets didn't explain how to use VLOOKUP for obtaining data from different files. Basically on the Invoice worksheet, in the cell under customer code use the VLOOKUP function to enter the customers name (from the table on the customers worksheet). In the cell under customers name use VLOOKUP to enter customers address, in the cell under customers address use VLOOKUP to enter suburb,state and postcode (from customer worksheet). Then I need to...

Help with database
I need to create a database that lists companies, with dates items done by, money paid, notes etc. Thats okay, but I need to retain the previous years records and start a new year off fresh. How would I do that? Jenni, It sounds like you have never created a database so I'm going to suggest you start here... Jeff Conrad's resources page... http://www.accessmvp.com/JConrad/accessjunkie/resources.html The Access Web resources page... http://www.mvps.org/access/resources/index.html A free tutorial written by Crystal (MS Access MVP)... http://allenbrowne.com/casu...

INSERTING ROWS and LOSING FORMAT
I own a spreadsheet that has daily data inputted for, say 7, years; to add rows to increase my size, I "Insert" rows at the top of the spreadsheet, say 10 rows every day! And, for years, only until recently, every new cell was properly formatted. Now, each time I "Insert" a rows, my column formatted (as todays time) as follows: 12:43am becomes 12:43, or 1:27pm becomes 1:27 and, this occurs EVERY DAY for 3 rows only of my newly "INSERTED" 12 to 14 rows! I have never had this happen before, and I...

is SQL 2000 architecture different SQL Server 2005 / 2008 architecture?
Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I have been told is a good book. I am going backwards... reading about the sql server architecture in chapter 3 of the book. As I'm reading... I was wondering is it different SQL Server 2000 vs SQL Server 2005/2008? Also where can I find a book/or link that can give me more details about the internals on architecture? Thank you norm (normanchan@gmail.com) writes: > Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I > have been told is a good book. I am g...

How can I group a set of documents needs to be opened repetatively
How can I group a set of documents needs to be opened repetatively? Hi Prithvi, You can open all the required documents and then choose File + Save WOrkspace. The next time you want them all open just open the Workspace. Judith -- Hope this helps "Prithvi" wrote: > How can I group a set of documents needs to be opened repetatively? ...

Moving customizations from SQL 2k to SQL 2k5 CRM
This may be a non-issue but I wanted to check to see if anyone has done this and ran into issues. I have the XML customization files from our previous install of CRM 3.0 with SQL 2k database. I now want to import and publish these same customizations to CRM 3.0 on SQL 2k5. Has anyone done this or can point me to documentation that tells about the success or issues for doing this? Thanks. On 7 Feb., 06:35, Chris Treanor <ChrisTrea...@discussions.microsoft.com> wrote: > This may be a non-issue but I wanted to check to see if anyone has done this > and ran into issues. > &...

Formulas
I have a huge database that I need to add fields and build a couple of formulas for those fields. Right now I have 3 months worth of data that I need to be able to report trending on. Within those 3 months, I have loan data breaking out how many loans have closed for a multitude of reasons. I need to trend how many each month close for those reasons and what they are as a percentage of the overall total of loans for that month. Any suggestions? To see how many closed in each month with each reason, you could just do a query that summarizes the data. I don't know what yo...

Inserting Attachment as Shortcut
In Office 97 you could select the way you want to Insert an Attachment. In Office 2003 the Insert Attachment as Shortcut option has been removed. Any ideas on how to Customize this option so that it will work in 2003. I don't want to insert the link I think you need this. 1. Click on Attachments 2. Select Attachment File (Don't Double click) 3. Click on Small Arrow on Right of Insert Button 4. Select Insert as HyperLink Best Regards, Luqman "stewartd" <stewartd@discussions.microsoft.com> wrote in message news:F0464305-F181-4781-A0AC-CE1919335339@microsoft.com...

how do i insert a radio button?
I hav writtne a survey in word and I would like to insert a "radio button" which can be clicked on tohighloight the preferred answer I take it from this that you have a raft of multiple choice questions? Do you want a button for each question, or do you want a button for each possible answer. If the latter, when do you want the button to highlight the correct answer? Will you be able to ensure that users will allow the essential macros to run? -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<...

Insert Formula and Copy to other cells
Hello, I need help fast! I need to be able to insert a Formula into rows that contain certain text. I then need to be able to copy all this formula across 15 columns and down to the end of the active workbook for each row that contains this criteria. I want the formula to automatically update to the correct cell reference. My formula is rather lengthy but it works perfectly in a 'normal' spreadsheet without running a macro. This is what I have: Dim intx as Integer Dim lngrow as long ActiveCell.SpecialCells(xlLastCell).Select lngrow = ActiveCell.Row 'lastcell in spreadsheet ...

Help window in Leopard stuck behind menu bar
Since installing Leopard when I open the Help window in any Office program it opens at the top of the desktop and seems to be behind the menu bar. The red, yellow and green buttons are missing/ not visible as well as the rest of the window header. It can't be moved or manipulated in any way. The real problem occurs when trying to close the window. Using keyboard short cuts it first acts on the active window. So in Excel it will close the workbook window when hitting command-W. Then it will work to close the help window. This slows everything down. Any help would be appreciated. Mike Go in...

How can i create a query on text input to return closest match?
I would like to create a text based parameter which will return the closest match? e.g Drink is the search citeria, the search will look for the word drink in every record and bring up those with the closest match Can you explain what you mean by closest match? Looking for the word Drink in a field and returning all the records where "drink" is included in the field would be simple. Field: SomeFieldWithText Criteria: LIKE "*" & [Enter word] & "*" But if you wanted to enter DRINK and find Drink or Drank or Drunk or Dink or Rink Or Dunk or .... in...

Help needed urgently
Hi, Im trying to check that one row of data satifies a criteria, then if it does it counts an entry in another column adjescent to it. Example: - Column A - Column B Blay - Y Blay - N Blay - Y Chop - N Chop - N High - Y What i would be asking (of a much bigger table) is If Column A = Blaydon and then column B (same row) = Y, then count. In this example I would want 2 returned as the answer. Is this possible? Thanks -- Andy Try one of these... Use cells to hold the criteria: D2 = Blaydon E2 = Y If you're using Excel 2007 or later: =COUNTIFS(A2:A7,...

Free & Busy Data messed up.. Plz Help!!
We have exchange v5.5. Recently one of the exchange admins was in the public folders messing around with permissions. He said he didn't change anything but something had to have changed because now all free and busy data, public address book, tasks, notes, etc are not accessible to new users. Any time we create a new email account they can't view there calendar (says it doesn't exist), nothing shows up in the global address list. Anytime they receive an email to accept a meeting it says unable to open item. I've done the usual things like reset the server. I loo...

Need to add check transactions to Microsoft Money using OFX file (not bank statements)
Does anybody have an example of an OFX file that imports check transactions into Microsoft Money? I need to simulate the manual input of checks. Dut to problems in our accounting software, it's best to enter transactions in our accounting system and rekey them into MS-Money. I found some "statement" files but I am thinking this isn't what I need to do????? I appreciate the depth and functionality of ofx vs. qif but I am finding it difficult to find ofx examples since they all appear to be produced by large companies (i.e. banks and financial companies) Any help wo...

user account disabled. need to redirect his e-mail
Hi, I have a situation: User left the company. I disabled his AD account. His boss asking to redirect this user new incoming e-mail to other user. Can you suggest an appropriate solution. Do I have to activate his account and just change a password or I can keep this account blocked. Thanks. Michael. On Sat, 7 Jan 2006 18:55:02 -0800, "MLA!" <MLA@discussions.microsoft.com> wrote: >Hi, >I have a situation: > >User left the company. >I disabled his AD account. >His boss asking to redirect this user new incoming e-mail to other user. > >Can you sugge...

CRM 3.0 Install Error with SQL Reporting Services and SQL 2005
Hi, Doing a CRM 3.0 Server installation. During the last page of the install wizard (or what I assume is the last page) when I goes through and does verification I get a red X next to SQL Reporting Services. The message I receive when I look at the details is: ***** The specified path is not a metabase path. Parameter name: path ***** Now SRS is installed and working. I put in the URL: http://<servername>/reportserver and every other variation on that I can find. Thoughts? Alan try http://servername/reports also assuming you did not use ssl... =======================...

SFO help
I have a quick question. My client's SFO button's wont install, and generally, their SFO installation is unstable. Given that we're hosting Exchange and CRM for the client and they're accessing our systems via VPN, does SFO work when their systems are not part of the domain that we've set up for them locally on our premises? Thanks!! Chris ...

sql summarizing help
Hi All, Given: table1 --------- id, cdate, weeksago, amount, typeid, timeclose 496, 2010-02-11, 0, 450, 3, 1 490, 2010-02-01, 1, 200, 4, 1 491, 2010-02-01, 1, 350, 2, 2 493, 2010-02-01, 1, 500, 5, 1 489, 2010-01-21, 3, 150, 2, 1 136, 2010-01-12, 4, 500, 3, 2 137, 2010-01-12, 4, 100, 3, 1 138, 2010-01-12, 4, 500, 2, 1 1. i'm trying to create a sql that will give me a summary resultset grouped by weeksago. 2. i want to sum the columns and get counts based on typeid. 3. avg of timeclose here's the sql i have: select weeksago, , (sum(timeClose)/(count(timeclo...

Vlookup help #2
I have two worksheets. Sheet 1 contains a number in column A and a name in column C. On Sheet 2 I put my data from a csv file. What I would like to do is vlookup Sheet 1 column C from the information on Sheet 2 and return the number in column A that is associated with the name in column C. Ex. Sheet 1 Sheet 2 A B C A B 33 N6-270-15 N6-270-75 35 34 N6-270-30 N...