sql query to create table if not exist
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: sql query to create table if not exist

  1. #1
    Join Date
    Jan 2008
    Posts
    13

    Talking sql query to create table if not exist

    syntax & example for sql query to create table if not exist in ms access database
    Thanks in advance

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: sql query to create table if not exist

    PHP Code:
    IF NOT EXISTS (SELECT FROM sys.objects WHERE object_id OBJECT_ID(N'[dbo].[Table]') AND type in (N'U'))
    CREATE TABLE [dbo].[Table](
        [
    Act_Id] [intIDENTITY(1,1NOT NULL,
        [
    Activity] [textNOT NULL ,
        [
    Date] [datetimeNOT NULL ,
        [
    User_ID] [nvarchar](20NOT NULL ,
     
    CONSTRAINT [PK_TABLEPRIMARY KEY CLUSTERED 
    (
        [
    Act_IdASC
    )WITH (PAD_INDEX  OFFSTATISTICS_NORECOMPUTE  OFFIGNORE_DUP_KEY OFFALLOW_ROW_LOCKS  ONALLOW_PAGE_LOCKS  ONON [PRIMARY]
    ON [PRIMARYTEXTIMAGE_ON [PRIMARY
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Jan 2008
    Posts
    13

    Talking Re: sql query to create table if not exist

    u have replied code in php . I need it in ms access. I am using front end as vb.net & back end as ms access. So please send it that i can use in ms access.
    Thanks in advance

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,868

    Re: sql query to create table if not exist

    The code posted is basic SQL. It will work for Access. It's not even remotely close to PHP, although he used [php] tags to post it.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Jul 2009
    Posts
    2

    Re: sql query to create table if not exist

    Quote Originally Posted by GremlinSA View Post
    PHP Code:
    IF NOT EXISTS (SELECT FROM sys.objects WHERE object_id OBJECT_ID(N'[dbo].[Table]') AND type in (N'U'))
    CREATE TABLE [dbo].[Table](
      {
    snip}
    ON [PRIMARYTEXTIMAGE_ON [PRIMARY
    Hey GremlinSA,

    What do the functions *_ID*, *N*, and *type* do? Could you explain a little about what's going on?

    Thanks,
    Joe

  6. #6
    Join Date
    Jul 2006
    Posts
    297

    Re: sql query to create table if not exist

    Quote Originally Posted by jp2code View Post
    What do the functions *_ID*, *N*, and *type* do? Could you explain a little about what's going on?
    Its a little easier to see whats going on if you run the query yourself and look at the results. Forget the WHERE clause he put in there and just run

    Code:
    USE [DatabaseName];
    SELECT * FROM sys.objects;
    You should see that this table holds a list of all the objects in the database consisting of Tables, Primary Keys, Foreign Keys, and other constraints. Each object has an ID associated with it.

    The *N* prefix to the string just allows the string to contain Unicode characters.

    The OBJECT_ID( ) just returns the corresponding id in the sys.objects table for the table you're looking for.

    Lastly the *type* isn't really a function its just another column in the database. In this situation N'U' just tells the query you're only interested in tables. You can add in the other types if you wish to the search but for this situation all you're interested in are the tables. Oh, and 'U' stands for USER_TABLE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center