Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a Table via T-SQL

Status
Not open for further replies.

LLudden

MIS
Jan 3, 2001
54
US
I have a software package I am doing a major update to. The original was written as an Access 2000 front end, and I am changing it that to VB6. The backend data is in SQL server 7.0

What I want to do is write T-SQL to transfer all of the existing data to a new database in SQL, and then add/drop/modify the columns as needed. I've gotten a pretty good start with the code below. The problem comes when I want to change the key lngAdjusterID and make it both the IDENTITY and PRIMARY KEY columns (the lines in red). Any help would be appreciated.

Code:
Dim oConn As ADODB.Connection
Dim oComm As ADODB.Command


Set oConn = New ADODB.Connection
Set oComm = New ADODB.Command

oConn.Open "Provider=sqloledb;" & _
           "Data Source=NS1;" & _
           "Initial Catalog=vbCoversheets;" & _
           "User Id=sa;" & _
           "Password="
 Set oComm.ActiveConnection = oConn
 oComm.CommandText = "SELECT * INTO tblAdjusters FROM Coversheets.dbo.tblAdjusters"
 oComm.Execute
 oComm.CommandText = "ALTER TABLE tblAdjusters ADD strNote TEXT"
 oComm.Execute
 oComm.CommandText = "ALTER TABLE tblAdjusters ADD lngAdjusterID INT"
 oComm.Execute
 oComm.CommandText = "UPDATE tblAdjusters SET lngAdjusterID = lngAdusterID"
 oComm.Execute
 oComm.CommandText = "ALTER TABLE tblAdjusters DROP COLUMN lngAdusterID"
 oComm.Execute
 oComm.CommandText = "ALTER TABLE tblAdjusters DROP COLUMN strHomePhone"
 oComm.Execute
 oComm.CommandText = "ALTER TABLE tblAdjusters DROP COLUMN strHomeFax"
 oComm.Execute
Code:
 oComm.CommandText = "ALTER TABLE tblAdjusters ALTER COLUMN lngAdjusterID ADD CONSTRAINT IDENTITY"
 oComm.Execute
 oComm.CommandText = "ALTER TABLE tblAdjusters ALTER COLUMN lngAdjusterID ADD CONSTRAINT PRIMARY KEY"
 oComm.Execute
Code:
 Set oComm = Nothing
 Set oConn = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top