1
DECLARE 
      @empid INT
    , @schedindate DATETIME
    , @ss NVARCHAR(100)
    , @indice NVARCHAR(3)
    , @FromDate DATETIME
    , @ToDate DATETIME
    , @TimeInR NVARCHAR(20)
    , @TimeOutR DATETIME,@day nvarchar(3)

SELECT 
      @FromDate = '20090114'
    , @ToDate = '20100130'

DECLARE @temp TABLE
(
      schedindate DATETIME
    , TimeInR VARCHAR(10)
    , TimeOutR VARCHAR(10)
    , empid INT
)
delete from PayrollTEST.dbo.[table1]
INSERT INTO @temp (schedindate, TimeInR, TimeOutR, empid)
SELECT DISTINCT
      schedindate
    , TimeInR
    , TimeOutR
    , empid 
FROM dbo.ta_timecard
WHERE schedindate BETWEEN @FromDate AND @ToDate

DECLARE @ids TABLE(id BIGINT IDENTITY(1,1), emp BIGINT)
INSERT INTO @ids (emp)
SELECT DISTINCT empid
FROM @temp

INSERT INTO PayrollTEST.dbo.[table1](id, EmpID)
SELECT id, emp
FROM @ids

DECLARE cc CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
    SELECT DISTINCT empid
    FROM @temp

OPEN cc
FETCH NEXT FROM cc INTO @empid

WHILE (@@fetch_status = 0) BEGIN

    SELECT
          @indice = (select DATEDIFF(day,@fromdate, t.SchedInDate))
        --, @TimeInR = t.TimeInR
        , @schedindate = t.SchedInDate
    , @day = dbo.ta_dayofweek(@schedindate)
    FROM @temp t
    WHERE empid = @empid
print(@indice)
print(@day)
print(@schedindate)
print(@TimeInR)
--    SELECT @ss = 'update table1 set nod' + @indice 
 --      + ' = '+ convert(nvarchar(3),@day) +' , din' 
  --     + @indice + ' = ' + @Schedindate + ' where empid = ' + CAST(@empid AS NVARCHAR(20))

    SELECT @ss = 'update table1 set nod' + @indice 
       + ' = '+ convert(nvarchar(3),@day) + ' where empid = ' + CAST(@empid AS NVARCHAR(20))

    EXEC  master.dbo.sp_executesql @ss

    FETCH NEXT FROM cc INTO @empid

END

CLOSE cc
DEALLOCATE cc

为什么我会收到此错误?我在正确的数据库中并且表存在。我很感激任何帮助

4

1 回答 1

-1

您确定您在正确的数据库中吗?您可以运行SELECT DB_NAME()以查看您当前的数据库

于 2013-05-29T08:04:27.977 回答