我在我的程序中有一个问题
错误代码:1064。您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“NULL”附近使用正确的语法
过程用于将dataData复制到一个DB到另一个DB
DELIMITER //
CREATE PROCEDURE COPY_DATA(demo_test VARCHAR(100),demo_copy_test VARCHAR(100))
BEGIN
DECLARE v_STRQRY VARCHAR(8000);
SET @STRQRY=CONCAT('
DECLARE @TBLCNT INT;
DECLARE @CNT INT;
DECLARE @COLCNT INT;
DECLARE @CNT1 INT;
DECLARE @COL_NAME VARCHAR(500);
DECLARE @DESTCOLNAME VARCHAR(8000);
DECLARE @SRCCOLNAME VARCHAR(8000);
DECLARE @STRQRY1 VARCHAR(8000);
DECLARE @STRQRY2 VARCHAR(8000);
DECLARE @DST_DB VARCHAR(200);
DECLARE @SRC_DB VARCHAR(200);
DECLARE @NEWLINE CHAR(1);
CREATE TABLE #IDENTITY_TBL (SL INT, NAME VARCHAR(100));
SET @NEWLINE = CHAR(13) + CHAR(10);
SET @DST_DB=''' , demo_copy_test, ''';
SET @SRC_DB= ''' , demo_test , ''';
USE ' , demo_copy_test , ';
EXEC SP_MSFOREACHTABLE "ALTER TABLE ? NOCHECK CONSTRAINT ALL";
USE MASTER;
SET @STRQRY1 =''INSERT INTO #IDENTITY_TBL (SL, NAME) SELECT ROW_NUMBER()OVER(ORDER BY S.NAME) SL ,O.NAME
FROM ''+ @DST_DB +''.SYS.OBJECTS O
INNER JOIN ''+ @DST_DB +''.SYS.SCHEMAS S ON S.SCHEMA_ID=O.SCHEMA_ID
WHERE O.type=''''U'''' And EXISTS(SELECT 1 FROM ''+ @DST_DB +''.SYS.COLUMNS WHERE OBJECT_ID=O.OBJECT_ID AND IS_IDENTITY=1);''
EXEC (@STRQRY1);
SET @CNT=1;
SET @CNT1=1;
CREATE TABLE #TABLE (SL INT, NAME VARCHAR(100),[DST_OWNER] VARCHAR(100),[SRC_OWNER] VARCHAR(100));
CREATE TABLE #COLTABLE (SL INT,DST_NAME VARCHAR(100),DST_COLTYPE INT ,DST_NULLABLE INT,SRC_NAME VARCHAR(100),SRC_COLTYPE INT,SRC_NULLABLE INT);
SET @STRQRY1=''
INSERT INTO #TABLE
SELECT ROW_NUMBER()OVER(ORDER BY T.NAME) SL ,T.NAME,T.OWNER [DST_OWNER],T1.OWNER [SRC_OWNER]
FROM (SELECT SO.*,SU.NAME OWNER FROM ''+ @DST_DB +''.SYS.TABLES SO INNER JOIN ''+ @DST_DB +''.SYS.SCHEMAS SU
ON SO.SCHEMA_ID = SU.SCHEMA_ID) T
INNER JOIN (SELECT SO1.*,SU1.NAME OWNER FROM ''+ @SRC_DB +''.SYS.TABLES SO1 INNER JOIN ''+ @SRC_DB +''.SYS.SCHEMAS SU1
ON SO1.SCHEMA_ID = SU1.SCHEMA_ID) T1 ON T.NAME=T1.NAME
WHERE T.TYPE=''''U'''';''
EXEC (@STRQRY1);
SET @TBLCNT=(SELECT COUNT (*) FROM #TABLE);
WHILE (@CNT<=@TBLCNT)
BEGIN
SET @COLCNT=0;
SET @CNT1=1;
DELETE FROM #COLTABLE;
SET @DESTCOLNAME='''';
SET @SRCCOLNAME='''';
SET @STRQRY2='''';
--INSERTING COLOUMN LIST TO TEMP TABLE
SET @STRQRY1=''
INSERT INTO #COLTABLE (SL,DST_NAME,DST_COLTYPE,DST_NULLABLE,SRC_NAME,SRC_COLTYPE,SRC_NULLABLE)
SELECT ROW_NUMBER()OVER(ORDER BY DST_NAME) SL,DST_NAME,DST_COLTYPE,DST_NULLABLE,SRC_NAME,SRC_COLTYPE,SRC_NULLABLE FROM
(SELECT D.NAME DST_NAME,SYSTEM_TYPE_ID DST_COLTYPE,IS_NULLABLE DST_NULLABLE
FROM ''+ @DST_DB +''.SYS.COLUMNS D
INNER JOIN ''+ @DST_DB +''.SYS.OBJECTS OB ON D.OBJECT_ID=OB.OBJECT_ID
WHERE OB.NAME = (SELECT NAME FROM #TABLE WHERE CONVERT(VARCHAR,SL)=''+CONVERT(VARCHAR,@CNT)+''))
Q1
LEFT JOIN (
SELECT D.NAME SRC_NAME,SYSTEM_TYPE_ID SRC_COLTYPE,IS_NULLABLE SRC_NULLABLE
FROM ''+ @SRC_DB +''.SYS.COLUMNS D
INNER JOIN ''+ @SRC_DB +''.SYS.OBJECTS OB ON D.OBJECT_ID=OB.OBJECT_ID
WHERE OB.NAME = (SELECT NAME FROM #TABLE WHERE CONVERT(VARCHAR,SL)=''+CONVERT(VARCHAR,@CNT)+''))
Q2
ON Q1.DST_COLTYPE=Q2.SRC_COLTYPE AND Q1.DST_NAME=Q2.SRC_NAME
WHERE (SRC_NAME IS NOT NULL) OR (SRC_NAME IS NULL AND DST_NULLABLE=0);
''
EXEC (@STRQRY1);
SET @COLCNT=(SELECT COUNT (*) FROM #COLTABLE);
WHILE (@CNT1<=@COLCNT)
BEGIN
SELECT @COL_NAME=DST_NAME FROM #COLTABLE W WHERE W.SL=@CNT1;
IF ((SELECT COUNT(1) FROM #COLTABLE D WHERE D.SL=@CNT1 AND D.SRC_NAME IS NULL AND D.DST_NULLABLE=0)=1)
BEGIN
SET @DESTCOLNAME=@DESTCOLNAME + '',['' + @COL_NAME + '']'';
END
ELSE IF ((SELECT COUNT(1) FROM #COLTABLE D WHERE D.SL=@CNT1 AND D.SRC_NAME IS NULL AND D.DST_NULLABLE=1)=1)
BEGIN
SET @DESTCOLNAME=@DESTCOLNAME + '','' ;
END
ELSE
BEGIN
SET @DESTCOLNAME=@DESTCOLNAME + '',['' + @COL_NAME + '']'';
END
IF ((SELECT COUNT(1) FROM #COLTABLE D WHERE D.SL=@CNT1 AND D.SRC_NAME IS NULL AND D.DST_COLTYPE IN (52,56,60,61,62,104,106,108,122,127,167,231) AND D.DST_NULLABLE=0)=1)
BEGIN
IF ((SELECT COUNT(1) FROM #COLTABLE D WHERE D.SL=@CNT1 AND D.DST_COLTYPE IN (52,56,60,62,104,106,108,122,127))=1)
BEGIN
SET @SRCCOLNAME=@SRCCOLNAME+ '','' + ''0'';
END
IF ((SELECT COUNT(1) FROM #COLTABLE D WHERE D.SL=@CNT1 AND D.DST_COLTYPE=61)=1)
BEGIN
SET @SRCCOLNAME=@SRCCOLNAME+ '','' + CHAR(39) + CONVERT(VARCHAR,GETDATE(),109) + CHAR(39);
END
IF ((SELECT COUNT(1) FROM #COLTABLE D WHERE D.SL=@CNT1 AND D.DST_COLTYPE IN (167,231))=1)
BEGIN
SET @SRCCOLNAME=@SRCCOLNAME+ '','' + + CHAR(39) + CHAR(39);
END
END;
ELSE IF ((SELECT COUNT(1) FROM #COLTABLE D WHERE D.SL=@CNT1 AND D.SRC_NAME IS NULL AND D.DST_NULLABLE=1)=1)
BEGIN
SET @SRCCOLNAME=@SRCCOLNAME + '','' ;
END
ELSE
BEGIN
SET @SRCCOLNAME=@SRCCOLNAME+ '',['' + @COL_NAME + '']'';
END;
SET @CNT1=@CNT1+1;
END
SET @DESTCOLNAME=RIGHT(@DESTCOLNAME, LEN(@DESTCOLNAME) - 1); -- REMOVING FIRST COMMAAA.
SET @SRCCOLNAME=RIGHT(@SRCCOLNAME, LEN(@SRCCOLNAME) - 1); -- REMOVING FIRST COMMAAA.
SET @DESTCOLNAME=CASE WHEN SUBSTRING(@DESTCOLNAME,LEN(@DESTCOLNAME),1)='','' THEN SUBSTRING(@DESTCOLNAME,0,LEN(@DESTCOLNAME)) ELSE @DESTCOLNAME END;
SET @SRCCOLNAME=CASE WHEN SUBSTRING(@SRCCOLNAME,LEN(@SRCCOLNAME),1)='','' THEN SUBSTRING(@SRCCOLNAME,0,LEN(@SRCCOLNAME)) ELSE @SRCCOLNAME END;
IF ((SELECT COUNT(1) FROM #TABLE V WHERE SL=@CNT AND EXISTS (SELECT 1 FROM #IDENTITY_TBL T WHERE T.NAME=V.NAME))=1)
BEGIN
--SET @STRQRY1 = ''SET IDENTITY_INSERT ''+ @DST_DB +''.''+ (SELECT DST_OWNER FROM #TABLE WHERE SL=@CNT)+ ''.'' + (SELECT NAME FROM #TABLE WHERE SL=@CNT) + ''''ON'''' '';
--SET @STRQRY2 =''SET IDENTITY_INSERT [PanAgroCore1].[PanAgro]. [ADM_ADDL_CHARGE_FORMULAS_MST] ''''ON''''; ''+ @NewLine;
SET @STRQRY2 =''SET IDENTITY_INSERT [''+ @DST_DB + ''].[''+ (SELECT DST_OWNER FROM #TABLE WHERE SL=@CNT) + ''].[''+ (SELECT NAME FROM #TABLE WHERE SL=@CNT) +''] ON; ''+ @NewLine;
SET @STRQRY1 = @STRQRY2 + ''INSERT INTO [''+ @DST_DB +''].['' + (SELECT DST_OWNER FROM #TABLE WHERE SL=@CNT)+ ''].['' + (SELECT NAME FROM #TABLE WHERE SL=@CNT) + ''] ('' + @DESTCOLNAME + '') SELECT '' + @SRCCOLNAME + '' FROM [''+ @SRC_DB +''].['' + (SELECT SRC_OWNER FROM #TABLE WHERE SL=@CNT)+ ''].['' + (SELECT NAME FROM #TABLE WHERE SL=@CNT) + ''];'' + @NewLine;
SET @STRQRY2 =''SET IDENTITY_INSERT [''+ @DST_DB + ''].[''+ (SELECT DST_OWNER FROM #TABLE WHERE SL=@CNT) + ''].[''+ (SELECT NAME FROM #TABLE WHERE SL=@CNT) +''] OFF; ''+ @NewLine;
SET @STRQRY1 = @STRQRY1 +@STRQRY2;
END;
ELSE
BEGIN
SET @STRQRY1 =''INSERT INTO [''+ @DST_DB +''].['' + (SELECT DST_OWNER FROM #TABLE WHERE SL=@CNT)+ ''].['' + (SELECT NAME FROM #TABLE WHERE SL=@CNT) + ''] ('' + @DESTCOLNAME + '') ''+ @NewLine +'' SELECT '' + @SRCCOLNAME + '' FROM [''+ @SRC_DB +''].['' + (SELECT SRC_OWNER FROM #TABLE WHERE SL=@CNT)+ ''].['' + (SELECT NAME FROM #TABLE WHERE SL=@CNT) + ''];'';
END;
EXEC(@STRQRY1);
PRINT @STRQRY1;
SET @CNT=@CNT+1;
END
DROP TABLE #TABLE;
DROP TABLE #COLTABLE;
DROP TABLE #IDENTITY_TBL;
USE ' , demo_copy_test , ';
EXEC SP_MSFOREACHTABLE "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL";
IF (@@ERROR=0)
BEGIN
USE MASTER;
ALTER DATABASE ', demo_test ,' SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE PanERP_Migration;
EXEC SP_RENAMEDB ', demo_test ,', PanERP_Migration;
ALTER DATABASE ', demo_copy_test ,' SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
EXEC SP_RENAMEDB ', demo_copy_test ,', ', demo_test ,';
END;
IF (@@ERROR=0)
BEGIN
--DROP DATABASE PanERP_Migration;
ALTER DATABASE ', demo_test , ' SET MULTI_USER;
ALTER DATABASE PanERP_Migration SET MULTI_USER;
PRINT ''ALL DATA COPIED SUCCESSFULLY TO NEW DATABASE''
END;
');
SET @stmt_str = v_STRQRY;
PREPARE stmt FROM @stmt_str;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- PRINT (@STRQRY);COPY_DATA
END;
//
DELIMITER ;
这里demo_test是一个sourceDB,demo_copy_test是DesignationDB
答案 0 :(得分:1)
您的My-Sql
存储过程语法错误。请遵循mysql网站的商店程序语法。 My-SQL有简单的语法,如Pl-SQL,Follwoing就是例子:
CREATE PROCEDURE `procedure`(IN `arg1` BIGINT, IN `arg2` BIGINT)
BEGIN
DECLARE var_1 BIGINT;
DECLARE var_2 VARCHAR(10);
BEGIN
/* INNER BLOCK */
END;
/* perform queries or you task */
END;
HeidiSQL
是mysql的好IDE。