How do I convert a SQL IF statement to a string?

I run this query in SQL and it works great.  It is to add a column if it doesn't exist in the existing table.  

IF NOT EXISTS (SELECT *                
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tblInventory' AND COLUMN_NAME = 'Image')
ALTER TABLE tblInventory ADD image IMAGE

I want to add it to my application code and I can't seem to get it to work.  Here is one of the MANY variations I have been trying:

Dim strTableAlter As String

Dim myConnT1 As SqlConnection = New SqlConnection("Server=" & machine & "\" & Inst & ";Trusted_Connection=True;")


strTableAlter = "USE [AddBook]" & _
                        "IF NOT EXISTS (SELECT *" & _
                        "FROM INFORMATION_SCHEMA.COLUMNS" & _
                        "WHERE [tblInventory] AND [IMAGE])" & _
                        "ALTER TABLE [tblInventory] ADD [image] [IMAGE]"


 Dim myCommandT2 As SqlCommand = New SqlCommand(strTableAlter, myConnT1)

Try
  myConnT1.Open()
  myCommandT2.ExecuteNonQuery()
  MsgBox("Your Household Inventory Table has been updated to the latest version.", MsgBoxStyle.OkOnly, "Table   Upgrade")

Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try

Any help is appreciated.
Thanks!!
Karen WilsonProgrammerAsked:
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.

Anthony PerkinsCommented:
You are missing semi-colons.  Try it this way:
strTableAlter = "USE [AddBook]; " & _
                        "IF NOT EXISTS (SELECT *" & _
                        "FROM INFORMATION_SCHEMA.COLUMNS" & _
                        "WHERE [tblInventory] AND [IMAGE]); " & _
                        "ALTER TABLE [tblInventory] ADD [image] [IMAGE]"
0
Karen WilsonProgrammerAuthor Commented:
I get an incorrect syntax near the keyword 'AND'  

This section has been causing the headache!

 "WHERE [tblInventory] AND [IMAGE]); " & _
0
Anthony PerkinsCommented:
Sorry it should be:
strTableAlter = "USE [AddBook]; " & _
         "IF NOT EXISTS (SELECT * " & _
          "FROM INFORMATION_SCHEMA.COLUMNS " & _
          "WHERE TABLE_NAME = 'tblInventory' AND COLUMN_NAME = 'Image'); " & _
                  "ALTER TABLE [tblInventory] ADD [image] [IMAGE]"

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Karen WilsonProgrammerAuthor Commented:
strTableAlter = "USE [AddBook]; " & _
                    "IF NOT EXISTS (SELECT * " & _
                    "FROM INFORMATION_SCHEMA.COLUMNS " & _
                    "WHERE TABLE_NAME = 'tblInventory' AND COLUMN_NAME = 'Image') " & _
                    "ALTER TABLE [tblInventory] ADD [image] [IMAGE]"

I removed the ; after 'Image' and it worked perfectly.  Thanks!  I was going crazy....  :O)
0
Anthony PerkinsCommented:
Glad you got it working.  Incidentally the image data type is deprecated and you really should replace it.
0
Karen WilsonProgrammerAuthor Commented:
What do you mean... I just started playing with Image.
0
Anthony PerkinsCommented:
Read up in the SQL Server Help for the image data type:
ntext, text, and image (Transact-SQL)
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
Visual Basic.NET

From novice to tech pro — start learning today.

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.