Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 3 tables
itemcreation table
Item_Name varchar(50),
Item_Code varchar(50),
Group_id int,(foreign key)
Unit_id int (foreign key)


itemgroup table
Group_Id int (primary key)
Group_Name varchar(50),
Chbox_Val varchar(50),
UnderGroup varchar(50)


Unit table
Unit_Id (primary key)
Unit_Name varchar(50),
Unit_Abbreviation varchar(50)

i want to insert Group_name,Undergroup from table itemgroup into table itemcreation
also unit_name from unit table into itemcreation table.

What I have tried:

i did this:
INSERT INTO tbl_ItemCreation SELECT @Item_Name,@Item_Code,t1.Group_Name,t1.underGroup ,t2.unit_Name
FROM tbl_ItemGroup t1,tbl_Unit t2
Inner JOIN tbl_ItemCreation t3 on t3.ItemCreation_Id=t1.ItemGroup_id
Inner Join tbl_ItemCreation t3 on t3.ItemCreation_Id=t2.Unit_Id

but getting error
Posted
Updated 2-Apr-16 4:34am
Comments
Karthik_Mahalingam 2-Apr-16 10:03am    
what error?

1 solution

The errors I got when trying to run this, and how I fixed each one:

1. Invalid object name 'tbl_ItemGroup' (et al)
This is entirely down to the way you have described your problem. I foolishly assumed you had used the real table names when you gave the schema. I had already created the tables so any sql posted below will be missing the "tbl_" on the table names.

2. The multi-part identifier "t1.ItemGroup_id" could not be bound.
Again, down to the schema you provided. In any sql below that column will be called Group_id

3. The correlation name 't3' is specified multiple times in a FROM clause.
This is a real problem. You can't use the alias t3 twice. So you either meant
C#
Inner JOIN ItemCreation t3 on t3.ItemCreation_Id=t1.Group_Id
Inner Join ItemCreation t4 on t4.ItemCreation_Id=t2.Unit_Id
or probably more likely meant
C#
Inner JOIN ItemCreation t3 on t3.ItemCreation_Id=t1.Group_Id
                AND t3.ItemCreation_Id=t2.Unit_Id


4. The multi-part identifier "t1.Group_Id" could not be bound.
At first I thought this was just down to the differences between the schema and the query you posted but then I spotted that you hadn't joined to Unit correctly
You need to use JOIN rather than the old syntax you have used. Otherwise you will need a WHERE clause to define how Unit should link to one of the other tables. So I looked again at the lines I mentioned in point 3 and concluded that what you actually meant was
SQL
INSERT INTO ItemCreation
SELECT @Item_Name,@Item_Code,t1.Group_Name,t1.underGroup ,t2.unit_Name
FROM ItemGroup t1
Inner JOIN ItemCreation t3 on t3.ItemCreation_Id=t1.Group_Id
inner join Unit t2 on t3.ItemCreation_Id=t2.Unit_Id


5. Not a syntax error here but you have no WHERE clause on this query - which means it is going to insert a new row for every row that is already on the table - I'm not sure that's what you really want to do either.

Get your errors fixed and try running your query several times. Check the behaviour is really what you want. If not then by all means come back with some sample data and an expected result.
 
Share this answer
 
v2
Comments
CHill60 4-Apr-16 9:52am    
As I said...some sample data from each table and your expected results from that sample will help us to help you
CHill60 6-Apr-16 5:58am    
That's fine - in point 5 of my solution I was pointing out that you were including the target table in the select.
I don't understand why you've started using old-style joins - don't. Use JOIN (with the appropriate left, right, inner, outer qualification).
Are you saying there is still a problem with your query? If so then give us some sample data from each table and the expected results that the sample data would give you. Help us to help you!
CHill60 6-Apr-16 6:33am    
That is a very long comment containing a lot of code which I am not going to read from a comment.
Sample data is what is required "In general, data is information: factual information, such as text, numbers, sounds, and images-anything that can be processed on a computer". Specifically "data" is not code.
You also haven't said what is wrong with your new query.

If you want help with this you have to
1) Tell us what the problem is
2) Provide some sample DATA
CHill60 6-Apr-16 7:04am    
You have a query that inserts data into your "main" table from other tables. Does it work? If not, what happens?
To get sample data try running
select top 10 from table2
select top 10 from table3
and share the results
From those rows give an example of what you would want inserted into table1.
I am not going to ask this again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900