• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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?
1 Solution
Bill BachPresidentCommented:
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.
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'))    
    -- Column Exists, add insert code here

Open in new window

holemaniaAuthor Commented:
Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now