可以在存储过程中建临时表,然后在ADO中调用呀。当然在ADO中也是可以建的。

解决方案 »

  1.   

    因为temp table 比较怪,它仅存在一个过程中,若你在vb或asp或C++等采用ado编制脚本或应用程序时中使用temp table 是会出错的,因为table 创建完已经被系统删去,故我的做法是存储过程中创建.
    create pro...
    ....
    create #temptable
    --use temptable
    drop #temptable
    ...
      

  2.   

    在一个连接cn下,可以创建temp table, 代码如下:
    Set cn = New ADODB.Connection
        cn.Open strcnn
        strSQL = "select stshop_shopid as shopid from site_shop where stshop_shopid like " & _
                "'" & ShopID & "%'"
        Set rs = New ADODB.Recordset
        rs.Open strSQL, cn, adOpenKeyset, adLockReadOnly, adCmdText
        n = rs.RecordCount
        ReDim Shop(1 To n)
        For i = 1 To n
            Shop(i) = rs!ShopID
            rs.MoveNext
        Next i
        rs.Close
        
        Set Cmd = New ADODB.Command
        Cmd.ActiveConnection = cn
        strSQL = "create table [#Temp]([style] char(10)  "
        For i = 1 To (n + 1) * 2
        strSQL = strSQL & "," & "[" & i & "] int default 0"
        Next i
        strSQL = strSQL & ")"
        Cmd.CommandText = strSQL
        Cmd.CommandType = adCmdText
        Cmd.Execute
        
     
        
        For i = 1 To n
            n_ShopID = Shop(i)
            strSQL = "insert into #temp(style,[" & 2 * i - 1 & "],[" & 2 * i & "])" & _
                    " select invstock_style,invstock_PreviousQuantity,invstock_Quantity" & _
                    " from inv_stock where invstock_shopid=" & "'" & n_ShopID & "'" & _
                    " and invstock_year=" & "'" & strYear & "'" & _
                    " and invstock_month=" & "'" & strMonth & "'" & _
                    " and (invstock_PreviousQuantity<>0 or invstock_Quantity<>0)"
            Cmd.CommandText = strSQL
            Cmd.Execute
       
        Next i
        
        sumCol1_qty = "[1]"
        sumcol2_amount = "[2]"
        For i = 2 To n
            sumCol1_qty = sumCol1_qty & "+" & "[" & 2 * i - 1 & "]"
            sumcol2_amount = sumcol2_amount & "+" & "[" & 2 * i & "]"
        Next i
        
        
        strSQL = "update #temp set " & "[" & (n + 1) * 2 - 1 & "]=" & sumCol1_qty & ",[" & _
                (n + 1) * 2 & "]=" & sumcol2_amount & ""
        Cmd.CommandText = strSQL
        Cmd.Execute
        
      

  3.   

       我觉得用Select Into,然后 Drop 也行吗
      

  4.   

    TO suinx:
      你写的程序错误太多了点吧!
      

  5.   

    ADO的Recordset本來就相當於一個Temp table的功能,如果不與後台的資料進行溝通的話,用Recordset足夠了。如果要使用Temp table. 關鍵在前端跟後台的Connection不能斷。下面是一個簡單的sample.
    Dim strSql As String
    Con.CursorLocation = adUseClient
    Con.Open "Provider=SQLOLEDB;UID=sa;PWD=;Initial Catalog=Var2000;Data Source=Nipsan"strSql = " Create Table #tmpTest(PartCode varchar(30),PartRule varchar(300))"
    Con.Execute strSql
    strSql = " INSERT INTO #tmpTest Select PartCode ,PartRule From tblstock"
    Con.Execute strSql
    只要Con不斷開,在這條連線上,#tmpTest都是有效的。
     Rs.Open " Select * From #tmpTest ", Con, adOpenStatic, adLockBatchOptimistic
      

  6.   

    connection.execute "select * into #tmpdb from mytable update #tmpdb set field1=1 where ........"
    SQLServer可以接收一组命令,其中就可以使用临时表(就像QueryAnalyzer),还可以使用事务(Begin Tran),不过这种方法效率没有存储过程高。
      

  7.   

    利用ADO,先在部件中引用ADO2.0 或 ADO2.5
    DIM RS AS ADO.RECORDSET
    SET RS=NEW ADO.RECORDSET
    RS.FIELDS.APPEND "ID",ADINTERGER,,ADFLDKEYCOLUMN
    RS.FIELDS.APPEND "DESCRIPTION",ADVARCHAR,40
    RS.OPEN LOCKTYPE:=ADLOCKBATCHOPTIMISTIC
    RS.ADDNEW
    RS.FIELDS(ID).VALUE=1
    RS.FIELD(DESCRIPTION).VALUE="FIRST RECORD"
    RS.UPDATE
    创建临时表就是这么简单.