Question : Do I have to select a predefined Identity field when using the SELECT INTO clause in a SQL Server stored procedure ?

I am developing an Access application using Access as the front end and SQL Server as the back end database. I use an ADP file.

I am using a SELECT INTO clause to populate an existing table that has an Identity field named UDLID to make the records unique. The reason I am using an Identity field is because there is more than 1 record
with the same Account Number.

The stored procedure starts out as follows:

CREATE PROCEDURE dbo.procUDLINTERNALMOD
AS

If  Exists(SELECT * FROM dbo.SYSOBJECTS WHERE NAME = 'tblUDLINTMOD' AND TYPE = 'U')
DELETE FROM tblUDLINTMOD
SET IDENTITY_INSERT  tblUDLINTMOD ON

Should I be using the last statement "SET IDENTITY_INSERT  tblUDLINTMOD ON" since I have an Identity field that needs to be populated ?

By creating my SELECT INTO statement, should I reference the Identity field, for ex:

SELECT UDLID = IDENTITY (int, 1, 1), ...

I am not clear on how to handle the populating of the Identity field named UDLID.

Do I need both:
a) SET IDENTITY_INSERT  (table name) tblUDLINTMOD ON
b) SELECT UDLID = IDENTITY (int, 1, 1), ...

Or is there another approach to use the SELECT INTO clause with an Identity field.

My ultimate objective is to get around the problem of populating a spreadsheet with the records from a
SQL Server table when the number of rows in the table exceeds 65,536 records.

The problem is that I am maintaining an Access 2003 application and using the TransferSpreasheet command to send the records to a spreadsheet. In Excel 2003, I can only transfer up to 65,536 records.

So I wanted to split up the table into segments of 65,536 records each and place these chunks into separate worksheets. I thought that if I used the SELECT INTO statement with a predefined table with
an IDENTITY field that I could accomplish this.  

Answer : Do I have to select a predefined Identity field when using the SELECT INTO clause in a SQL Server stored procedure ?

This will work from access to export directly to excel
Function ExportToExcel()
Dim xl As Excel.Application
Dim xlWB As Excel.Workbook
Dim sht As Excel.Worksheet, rng As Excel.Range
Dim db As DAO.DataBase, rs As DAO.Recordset
Dim recordtotal As Long
Dim SheetNum As Long
 
Set db = CurrentDb
db.Execute "Select * into CustomerTemp from Customer", dbFailOnError
recordtotal = DCount("CustomerID", "CustomerTemp")
Set xl = CreateObject("Excel.Application")
Set xlWB = xl.Workbooks.Open("C:\test1.xls")  '<--Your Excel File Here
xl.Visible = True
SheetNum = 1
Do While recordtotal > 0
    Set rs = db.OpenRecordset("Select top 60000 * from CustomerTemp;")  '<--Your Table or Query here
    If rs.EOF Then Exit Function
    rs.MoveFirst
        xlWB.Worksheets.Add after:=Worksheets(Worksheets.count)
        ActiveSheet.Range("A1").CopyFromRecordset rs
        Set sht = ActiveSheet
        sht.Name = "YourSheet" & SheetNum
        SheetNum = SheetNum + 1
        db.Execute "delete * from CustomerTemp where CustomerID in(Select top 60000 customerid from CustomerTemp;)", dbFailOnError
    rs.Close
    recordtotal = DCount("CustomerID", "CustomerTemp")
Loop
xlWB.Close (True)
xl.Quit
Set xl = Nothing
docmd.deletetable "CustomerTemp"

You just need to change to your table or query names.
J
End Function
Random Solutions  
 
programming4us programming4us