SQL Insert Help - Fields not exists in some databases

I wrote a VB.NET application that simply do some inserts.  Issue I'm running into is that we have old and new databases for this off the shelf ERP software.  The new databases have a few extra fields that does not exists in the old database.

Is it possible to use the same insert and insert the field if it exists and not insert if it doesn't?

insert into master_item(id, description, bin_id)
values('99232', '3.4x3.3 ALX 20032', '234')

The above example, the "bin_id" does not exists in the old database.  I'm using the vb.net program to insert items into each database via a loop.  I would hate to create 2 separate query strings for this.  Could it be modify so that one query string would work?
holemaniaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill BachPresident and Btrieve GuruCommented:
No.  If you attempt to insert a field which does not exist, the query does (and should) fail.  What you COULD do is leave off the field, in which case the new database will get a NULL there (or default value, if assigned), but I am not sure this is what you want.
0
Ess KayEntrapenuerCommented:
Bill is correct.

if the field doesnt exist, it will fail

but if there are extra fields, it shouldnt be a problem



TABLE fields:
field1 field2

insert into TABLE (field1) values (1)     = GOOD
insert into TABLE (field3) values (3)     = FAIL





the workaround is this




if exists(select * from sys.columns 
            where Name = N'columnName' and Object_ID = Object_ID(N'tableName'))    
begin
    -- Column Exists, add insert code here
end

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
holemaniaAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.