0

我的 tsql 需要一些帮助,我了解 sql,但主要只是查询表,我发现 tsql 和循环很难完成。我的 IF 语句遇到了症结所在。如您所见,我已经编写了循环并尝试了一些调试,并且到目前为止代码是正确的。

在 Else 部分中,我需要检查它是否是第一次使用 @firstloop 通过循环,然后复制所有数据,这主要是我遇到问题的循环,并且可以帮助获得起点。

我正在使用游标,但被告知它不会影响性能,因为它没有做太多工作,我将不胜感激有关问题的任何帮助。我还被告知某些部分是硬编码的,例如 Production2 应该是 @Source_Database_Name .

 declare @Source_Database_Name varchar(255) = 'Production2';
declare @Destination_Database_Name varchar(255) = 'c365online_script1';

declare @Company_Id int = 1 --declare a companyid 

CREATE TABLE #CompanyID (ID bigint)

INSERT INTO #CompanyID(ID)
    VALUES('15')


--FIRST CURSOR LOOP THROUGH THIS TABLE
CREATE TABLE #TableList (
    processorder int,
    tablename NVARCHAR(100)
    )
INSERT INTO #TableList (processorder, tablename )
VALUES
(1, 'tProperty');

DECLARE @firstLoop BIT
--SET @firstLoop = true
DECLARE @Counter INT  -- counting variable

----------- Cursor specific code starts here ------------
-- company cursor
declare copyCompanyDataCursor CURSOR fast_forward FOR
SELECT ID from #CompanyID;

open copyCompanyDataCursor
fetch next from copyCompanyDataCursor into @Company_Id;

WHILE @@FETCH_STATUS = 0
    BEGIN
        declare @processorder int;
        declare @tablename varchar(500);
        -- table cursor

        declare copyTableDataCursor CURSOR fast_forward FOR
        SELECT processorder,tablename from #TableList order by processorder;

        open copyTableDataCursor
        fetch next from copyTableDataCursor into @processorder, @tablename;

        while @@FETCH_STATUS = 0
        BEGIN
            SET IDENTITY_INSERT [c365online_script1].[dbo].[tCompany] ON

            -- Does the table have a companyID column? if statement checking for company id
            IF EXISTS(SELECT *  FROM Production2.INFORMATION_SCHEMA.COLUMNS
                        WHERE COLUMN_NAME='CompanyID' and TABLE_NAME=@tablename)
            BEGIN
                declare @debug varchar(max)             
                SET @debug = 'INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + CAST(@Company_Id as varchar(10))
                print @debug
                --EXEC(@debug)
                --EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + @Company_Id )

            END
            ELSE
            -- if no check if this is the first time through company loop and copy all data- if @firstloop company exists look at information schema
            BEGIN 
                    Print 'No'
            END 
            -- if yes then copy data based on companyID in cursor  



                    --EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename )
                    -- company logic


            SET IDENTITY_INSERT [c365online_script1].[dbo].[tCompany] OFF

            FETCH NEXT FROM copyTableDataCursor into @processorder,@tablename;
        END

        close copyTableDataCursor;

        Deallocate copyTableDataCursor;
4

1 回答 1

1

不知道你为什么使用光标。要回答您的“检查它是否第一次通过循环”的问题,那么下面是对此的解决方案。

在“while @@FETCH_STATUS = 0”行上方声明一个变量。像“Firsttime = 0”这样的东西

在 IF 下,“Firsttime = 1”表示这不是第一次。

在 ELSE 中,检查 ---> if "Firsttime = 0" 则表示第一次 ---> if "Firsttime = 1" 则表示不是第一次

于 2013-10-31T12:32:41.207 回答