-1

I need help to check if a sql 2008 database contains a table named company ID if not it must create it. I was originally asked to do this sql side but now I was asked to do it vb.net side and I have no vb experience as I am a sql programmer.

I was given this for the connection string:

Dim SQLConnection As New SqlClient.SqlConnection
      (SQL_Loader("", My.Settings.SQL_Win_Auth, 
                      My.Settings.SQL_Username,
                      My.Settings.SQL_Password,
                      My.Settings.SQL_Server_Name,
                      My.Settings.SQL_DB_Name))

Check_Data_Base(SQLConnection.ConnectionString)
stuartd
  • 70,509
  • 14
  • 132
  • 163
user2564223
  • 441
  • 1
  • 4
  • 7
  • 2
    You can use [this question](http://stackoverflow.com/questions/167576/sql-server-check-if-table-exists) to see if the table exists. If it doesn't, you can execute a `CREATE TABLE` command. – stuartd Jul 10 '13 at 09:54
  • It dont work i tried that – user2564223 Jul 10 '13 at 09:59
  • 1
    What did you try? It what way does it dont work? Do you get an error? If so, which one? – stuartd Jul 10 '13 at 09:59
  • I tried this: Dim Command As New SqlClient.SqlCommand("IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Pastel_Companies]') AND type in (N'U')) BEGIN()CREATE TABLE [dbo].[Pastel_Companies](PersonID int, ID int) END") – user2564223 Jul 10 '13 at 10:02
  • it does nothing. No database is created – user2564223 Jul 10 '13 at 10:04
  • To check if a table exits you have the question that @stuartd mentioned, so if you have a specific problem edit your question and show us your concrete error and tell us why it doesnt work like you want. – SysDragon Jul 10 '13 at 10:08
  • Does the connection you're using have 'ALTER' rights to the db? – stuartd Jul 10 '13 at 10:10
  • select * from fn_my_permissions(NULL, 'DATABASE') – user2564223 Jul 10 '13 at 10:13

1 Answers1

0

create a sample table in the database with coloumns you want

Here sample table is chatsample

create procedure [dbo].[new_table](@tbName varchar(50)) as
begin
declare @Sql nvarchar(max)

set @Sql='if NOT EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'''+@tbName+''') AND type in (N''U''))' + CHAR(10) +
       'begin' + CHAR(10) +
         'select * into '+ @tbName+ ' from ChatSample'+CHAR(10)+
         'end'+Char(10)         

exec(@Sql)
end

Kavipriya
  • 54
  • 4