473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

SQL Create Table?

Is there a tool that can take an Access database and generate SQL
"CREATE TABLE" statements for all the tables in it?
Nov 13 '05 #1
3 3855
"blindsey" <bl******@dsicdi.com> wrote in message
news:f9************************@posting.google.com ...
Is there a tool that can take an Access database and generate SQL
"CREATE TABLE" statements for all the tables in it?


There are several products that will reverse-engineer Jet (and other)
databases. MS Office Visio is one.
Nov 13 '05 #2
blindsey wrote:
Is there a tool that can take an Access database and generate SQL
"CREATE TABLE" statements for all the tables in it?


the code below is not finished/polished, but it's a start.
Watch for wrapping... for your purpose, just call while looping
tabledefs, e.g.

For each tdf in db.tabledefs
DoSomethingWith CreateSQLTable(tdf.name)
next

Can always modify it to take tdf as parameter.

Function CreateSQLTable(pstrTableName As String, Optional pfGrantAll As
Boolean = False)
On Error GoTo CreateSQLTables_Err
' Returns T-SQL create table statement for an Access table
' © T.Best 2000-2004
' You may distribute/modify this as part of an application.
' May need additional work to handle more field types and unique
indices

Dim e As Error, strErrMsg As String
Dim db As Database, tdf As TableDef, fld As Field, idx As Index
Dim strSQL As String
Dim strSQLTable As String
Dim strField As String
Dim strSQLCreateTable As String
Set db = CodeDb()
strSQLTable = pstrTableName
Set tdf = db.TableDefs(pstrTableName)
' Build new
strSQL = ""
strSQL = strSQL & "create table " & strSQLTable & " (" & vbCrLf
For Each fld In tdf.Fields
strField = " [" & fld.Name & "] "
Select Case fld.Type
Case dbText
strField = strField & "varchar(" & fld.Size & ")"
Case dbDate
strField = strField & "datetime"
Case dbCurrency
strField = strField & "money"
Case dbLong
strField = strField & "int"
If fld.Attributes And dbAutoIncrField Then
strField = strField & " identity (1,1)"
End If
Case dbMemo
strField = strField & "text"
Case dbBoolean
strField = strField & "bit"
Case Else
Err.Raise 30001, , "Unknown field type ctrating table "
& strSQLTable & "." & fld.Name
End Select

If fld.Required Or fld.Attributes And dbAutoIncrField Then
strField = strField & " NOT NULL"
Else
strField = strField & " NULL"
End If
strSQL = strSQL & strField & "," & vbCrLf
Next
' strip off trailing "," & vbcrlf
strSQL = Left(strSQL, Len(strSQL) - 3)

strSQL = strSQL & ")"

strSQLCreateTable = strSQL

' Primary keys
For Each idx In tdf.Indexes
If idx.Primary Then
strSQL = "alter table " & strSQLTable & " with nocheck add"
& vbCrLf
strSQL = strSQL & " constraint [aaPK_" & strSQLTable & "]
primary key nonclustered" & vbCrLf
strSQL = strSQL & " (" & vbCrLf
For Each fld In idx.Fields
strSQL = strSQL & " [" & fld.Name & "]" & vbCrLf
Next
strSQL = strSQL & " ) with fillfactor = 90"
End If ' is PK
Next

strSQLCreateTable = strSQLCreateTable & vbCrLf & strSQL

' other indices
For Each idx In tdf.Indexes
If Not idx.Primary Then
' need a bit of work here to determine unique indices
strSQL = "create index " & idx.Name & " on " & strSQLTable
& "("
For Each fld In idx.Fields
strSQL = strSQL & "[" & fld.Name & "],"
Next
' strip trailing ",
strSQL = Left$(strSQL, Len(strSQL) - 1)
strSQL = strSQL & ") with fillfactor=90"
strSQLCreateTable = strSQLCreateTable & vbCrLf & strSQL

End If ' not PK

Next idx

' grant permissions
If pfGrantAll Then
strSQL = "grant select,insert,update,delete on " & strSQLTable
& " to public"
strSQLCreateTable = strSQLCreateTable & vbCrLf & strSQL
End If

CreateSQLTable = strSQLCreateTable
CreateSQLTables_Exit:
On Error Resume Next
Set idx = Nothing
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
Exit Function
CreateSQLTables_Err:
Select Case Err
Case 3146 ' ODBC
For Each e In DBEngine.Errors
strErrMsg = strErrMsg & "#ODBC Error " & e.number & " -
" & e.Description & vbCr
Next
MsgBox strErrMsg, vbExclamation, "Error " & Err.number & "
in Import2SQL()"
Case Else
MsgBox Err.Description, 16, "Error #" & Err & " In
CreateSQLTables()"
End Select
Resume CreateSQLTables_Exit
Resume
End Function
--
Pretentious? Moi?
Nov 13 '05 #3
bl******@dsicdi.com (blindsey) wrote in message news:<f9************************@posting.google.co m>...
Is there a tool that can take an Access database and generate SQL
"CREATE TABLE" statements for all the tables in it?


Hi!

There's the possible way how to do what you need in CASE Studio 2.

http://www.casestudio.com

You will have to do the following steps: reverse engineer an Access
database, convert it into another database, e.g. Oracle (I do not know
what database you need to generate the SQL script for) and then you
can generate SQL script automatically.

Feel free to test it in the demo version which is for free on:
http://www.casestudio.com/enu/download.aspx

Regards,

Vladka Sikorova
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew | last post by:
I have a problem creating mySQL tables with PHP. I am making an app where a user can create a project. Pressing "submit" on proj_form.php goes to proj_add.php where a couple of things happen. ...
0
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
4
by: Michael Jackson | last post by:
I have a stored procedure to create a table. In my program I want the user to name the table to be created, therefore I pass a parameter to the SP for the table name. I cannot get it to work. It...
2
by: Karen Sullivan | last post by:
Hi, all. I'm fairly new to SQL, and I have been trying to create a table from a text file. I have been looking at this for days, and can't find the problem. I get a syntax error " Line 55:...
6
by: Bruce | last post by:
I want to create a new table based on an existing table, but I don't want the tables to have any enforced relationship. Is this possible without having to do a CREATE TABLE and an INSERT? ...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
6
by: Peter Nurse | last post by:
For reasons that are not relevant (though I explain them below *), I want, for all my users whatever privelige level, an SP which creates and inserts into a temporary table and then another SP...
24
by: Dan2kx | last post by:
Hello to all that read and thank you to all that post Background: I have been tasked to create a holiday database for the employees in my lab, (so expect many more posts) im stuck at the first...
3
by: SteveP26 | last post by:
Hi guys, I keep getting this error message (below) when I try to run the sql query for my database Heres the code, but the error message only applies to the LAST table (SalesCopy), I have no...
2
jbt007
by: jbt007 | last post by:
All, Access 2003 - WinXP I thought this would be a no brainer, but it seems to be a perplexing problem. I have a simple table I use for importing several text reports, use VBA to run through...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.