Results 1 to 22 of 22

Thread: create column in sql server

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Exclamation create column in sql server

    Hi,

    Anyone know is it possible to write a macro that can create new column in sql server and after created the newly column, insert the data into the new column?

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: create column in sql server

    Search Google on "SQL ALTER TABLE Statement"
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    this is how i use to insert. advice me how to change it or make it to ALTER TABLE Statement from

    Code:
    strSQL = "Insert INTO [odbc;Driver={SQL Server};" & _
        "Server=D1406;Database=OP;" & _
        "UID=coc;PWD=pd1].TestDB " & _
        "Select * FROM [Insert$]"
    TO

    Code:
    ALTER TABLE Persons
    ADD [2009-10-Wk1] date
    
    ALTER TABLE Persons
    ADD [2009-10-Wk2] date
    
    ALTER TABLE Persons
    ADD [2009-10-Wk3] date
    
    ALTER TABLE Persons
    ADD [2009-10-Wk4] date
    
    ALTER TABLE Persons
    ADD [2009-10-Wk5] date
    and

    after creating columns how can i make it insert to my new column like in my screenshot. i already square out the location.

    Thanks for your respone
    Last edited by Waka; Nov 17th, 2009 at 01:43 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    i tried using this codes its not woking

    Code:
    strSQLadd = "ALTER TABLE[odbc;Driver={SQL Server};" & _
        "Server=D1406;Database=OP;" & _
        "UID=coc;PWD=pd1].TestDB " &_
        "ADD ( 2009-10-Wk1 float(8),  2009-10-Wk2 float(8),  2009-10-Wk3 float(8),  2009-10-Wk4 float(8), 2009-10-Wk5 float(8) )"
    Last edited by Waka; Nov 16th, 2009 at 10:49 PM.

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: create column in sql server

    Answer to your 1st question in post 3

    If "2009-10-Wk1" is the Column Name then change this ALTER TABLE Persons ADD [2009-10-Wk1] date to

    ALTER TABLE Persons ADD 2009-10-Wk1 date

    Answer to your 2nd question in post 3

    If you want to add a Column with the name "DateOfBirth" then try this

    ALTER TABLE Persons ADD DateOfBirth date
    Last edited by Siddharth Rout; Nov 17th, 2009 at 12:24 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    how can i do it in macro ? i would like to create a new column before insert the new data in that rows. like figure 1 to figure 3.

    this code is not working, for my macro.
    Code:
    strSQLadd = "ALTER TABLE[odbc;Driver={SQL Server};" & _
        "Server=D1406;Database=OP;" & _
        "UID=coc;PWD=pd1].TestDB " &_
        "ADD ( 2009-10-Wk1 float(8),  2009-10-Wk2 float(8),  2009-10-Wk3 float(8),  2009-10-Wk4 float(8), 2009-10-Wk5 float(8) )"
    follow by

    Code:
    strSQL = "Insert INTO [odbc;Driver={SQL Server};" & _
        "Server=D1406;Database=OP;" & _
        "UID=coc;PWD=pd1].TestDB " & _
        "Select * FROM [Insert$]"
    Attached Images Attached Images    
    Last edited by Waka; Nov 17th, 2009 at 01:41 AM.

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: create column in sql server

    Ok here is an example which is untested

    Check first it if it adds a column...

    Code:
    Private Sub Command1_Click()
        Dim conn As ADODB.Connection, rs As ADODB.Recordset
        Dim sqlStatement As String
        
        '~~> In case there is error
        On Error GoTo ErrHandler:
    
        Set conn = New ADODB.Connection
        Set rs = New ADODB.Recordset
        
        '~~> Connection String
        conn.ConnectionString = _
        "DRIVER={SQL Server};" _
        & "SERVER=D1406;" _
        & "DATABASE=OP;" _
        & "UID=coc;" _
        & "PWD=pd1;"
    
        conn.CursorLocation = adUseClient
        conn.Open
        
        '~~> Add the column to the table "MyTableName" Amend as required
        sqlStatement = "ALTER TABLE MyTableName ADD [2009-10-Wk1] float(8)"
        
        Call conn.Execute(sqlStatement)
        
        '~~> Close the recordset and connection
        rs.Close
        Set rs = Nothing
        conn.Close
        Set conn = Nothing
        
    ErrHandler:
        '~~> Error Handling
        MsgBox Err.Number & ": " & Err.Description
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    the code works but the there is an error too and the data type in my SQL server is 'real' instead of float.
    Last edited by Waka; Nov 18th, 2009 at 10:00 PM.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: create column in sql server

    Moved To Database Development

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create column in sql server

    Quote Originally Posted by Waka View Post
    the code works but the there is an error too
    Which line of code did it occur on?

    I presume it was on rs.Close , because the recordset is never opened. If so, that line should be removed (and anything else that refers to rs can be too).
    and the data type in my SQL server is 'real' instead of float.
    That is fine - they are just different names for the same thing.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    Quote Originally Posted by si_the_geek View Post
    Which line of code did it occur on?
    at the end of the line encountered error
    Code:
    End sub
    i remove rs.Close, no more error.
    Last edited by Waka; Nov 17th, 2009 at 10:09 PM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    i using the nomarl insert statment i got ther result in 'fig 1' but i would like to make it like my figure 3, how can i make the insert statment into my figure 3 ?
    Attached Images Attached Images   
    Last edited by Waka; Nov 18th, 2009 at 12:22 AM.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create column in sql server

    Quote Originally Posted by Waka View Post
    at the end of the line encountered error
    Code:
    End sub
    i remove rs.Close, no more error.
    Ah.. I should have pointed out that to find out which line caused the error you need to disable the error hander, by adding ' at the start of the On Error line.

    However, as the fix was valid - it means my suspicion was correct.
    i using the nomarl insert statment i got ther result in 'fig 1' but i would like to make it like my figure 3, how can i make the insert statment into my figure 3 ?
    Don't use Insert, because that explicitly adds a new record.

    To alter existing records, use Update.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    i using this code on it but, its not working. please advice me how to make the update statment works. sorry about this i'm newbie. thanks for reply

    Code:
    strSQL = "Insert INTO [odbc;Driver={SQL Server};" & _
        "Server=D1406;Database=OP;" & _
        "UID=coc;PWD=pd1].TestDB " & _
        "Update TestDB set * FROM [Insert$]"
    Attached Images Attached Images  
    Last edited by Waka; Nov 18th, 2009 at 10:04 PM.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    Hi,

    i'm using this statemen but its not working.

    Code:
    sql = "Update TestDB SET "
        sqlStatement = sql & "2009-11-Wk1 = '" & Worksheets("Forecast").Range("E2:E6").Value"' "

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create column in sql server

    Rather than post something almost meaningless like "not working" and make us guess what it means in this particular case (which takes time, and can be wrong), actually tell us what is happening. eg:
    • is there an error? if so what was it? and which line was it on?
    • if variables are involved, what did they contain?
    • ...

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    there is only an error msg 'End statement'.

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create column in sql server

    Was that the entire message?

    Was a line of code highlighted? (if so, what was it?)

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    Code:
    sql = "Update TestDB SET "
        sqlStatement = sql & "2009-11-Wk1 = '" & Worksheets("Forecast").Range("E2:E6").Value"'
    in proper End statement.

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create column in sql server

    Quote Originally Posted by Waka View Post
    in proper End statement.
    Er, what?

    Please tell us the entire error message you are getting.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2009
    Location
    Singapore
    Posts
    355

    Re: create column in sql server

    This error has shown
    Attached Images Attached Images  

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create column in sql server

    "End statement" is very different to "Expected: end of statement", and far less informative. Now we know what the problem is, we can help solve it.

    The problem is caused by not joining strings properly. Rather than this:
    Code:
        sqlStatement = sql & "2009-11-Wk1 = '" & Worksheets("Forecast").Range("E2:E6").Value"'
    ..you should have this:
    Code:
        sqlStatement = sql & "2009-11-Wk1 = '" & Worksheets("Forecast").Range("E2:E6").Value & "'"

Posting Permissions

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



Click Here to Expand Forum to Full Width