2017-11-09 5 views
0
alter procedure inselt 

@MES VARCHAR(30), @IDA INT 
AS BEGIN 
DECLARE @NRODIA INT 
DECLARE @NOMBRE_TABLA VARCHAR (50),@ENTRADA VARCHAR(20),@SOPA VARCHAR(20),@PLATO VARCHAR(20) 
DECLARE @PLATOP varchar (50) 
DECLARE @PLATOS varchar (20) 
SET @PLATOP='PLATOP' 
set @PLATOS='PLATOS' 
DECLARE @A VARCHAR(1) 
SET @A=CHAR(39) 
DECLARE @tipo varchar(20) 
SET @tipo='tipo' 
SET @[email protected] 
declare @entrada1 varchar(20) 
set @ENTRADA1='entrada' 
declare @sopa1 varchar(20) 
set @sopa1='sopa' 
declare @plato1 varchar(20) 
set @plato1='plato' 
declare @nd int 
DECLARE @xEntrada VARCHAR(20), @xSopa varchar(20), @xPlato varchar(20) 
set @nd=1 
IF OBJECT_ID(@NOMBRE_TABLA) IS NULL 
BEGIN 
EXECUTE ('CREATE TABLE ' + @NOMBRE_TABLA+' 
(nrodia integer,entrada varchar(20), sopa varchar(20), plato varchar(20),unique(entrada,sopa,plato))') 

while @nd<31 
begin 
EXECUTE(
    'Select TOP 1' + @xEntrada + '=' [email protected]+ 'from' [email protected]+ 'where' [email protected]+ '=' [email protected][email protected][email protected]+ 'ORDER BY NEWID() 
    Select TOP 1' + @xSopa + '=' [email protected]+ 'from' [email protected]+ 'where' [email protected]+ '=' [email protected][email protected][email protected]+ 'ORDER BY NEWID() 
    Select TOP 1' + @xPlato + '=' [email protected]+ 'from' [email protected]+ 'where' [email protected]+ '=' [email protected][email protected][email protected]+ 'ORDER BY NEWID() 
    if (not exists(select 1 FROM'[email protected]_TABLA+ 'WHERE' [email protected]+ '=' [email protected]+ 'AND' [email protected]+ '=' [email protected]+ 'AND' 
[email protected]+ '=' [email protected]+ '))' + 'BEGIN INSERT INTO' [email protected]_TABLA+ 'values (' [email protected]+ ',' [email protected]+ ',' [email protected]+ ',' 
[email protected]+ ')' + 'SET' [email protected]+ '=' [email protected]+ '+1' + 'END END;') 
end 
END 
END 

でそれを埋めることができます数字」 2)作成されたテーブルが 値がこの表から来た別のテーブルから値を満たさなければならないことをした後:どのようにテーブルを自動生成するプロシージャを作成し、自動的にこれがへの私の試みである値

CREATE TABLE PLATOS(Idplatos integer PRIMARY KEY, Tipo varchar(20), PlatoP varchar(20)); 
---insert--- 
INSERT INTO Platos VALUES('1','entrada','ceviche'); 
INSERT INTO Platos VALUES('2','entrada','arrozleche'); 
INSERT INTO Platos VALUES('3','sopa','caldo'); 
INSERT INTO Platos VALUES('4','sopa','mote'); 
INSERT INTO Platos VALUES('5','plato','lomo'); 
INSERT INTO Platos VALUES('6','plato','rusa'); 

、それはだ手続きなしコード:

declare @nd int 
set @nd=1 
DECLARE @xEntrada VARCHAR(20), @xSopa varchar(20), @xPlato varchar(20) 
while @nd<=31 
begin 
    Select TOP 1 @xEntrada = PlatoP from Platos where tipo = 'entrada' oRDER BY NEWID() 

    Select TOP 1 @xSopa = PlatoP from Platos where tipo = 'sopa' Order by NewId() 
    Select TOP 1 @xPlato = PlatoP from Platos where tipo = 'plato' oRDER by NewId() 

    IF (NOT EXISTS(SELECT 1 FROM abril2017 WHERE Entrada = @xEntrada AND Sopa = @xSopa AND Plato = @xPlato)) 
    BEGIN 
     Insert into abril2017 values (@nd,@xEntrada,@xSopa,@xPlato) 
     SET @[email protected]+1 
    END 
END; 
をそのコードを変換

EXECUTE(
    'Select TOP 1' + @xEntrada + '=' [email protected]+ 'from' [email protected]+ 'where' [email protected]+ '=' [email protected][email protected][email protected]+ 'ORDER BY NEWID() 
    Select TOP 1' + @xSopa + '=' [email protected]+ 'from' [email protected]+ 'where' [email protected]+ '=' [email protected][email protected][email protected]+ 'ORDER BY NEWID() 
    Select TOP 1' + @xPlato + '=' [email protected]+ 'from' [email protected]+ 'where' [email protected]+ '=' [email protected][email protected][email protected]+ 'ORDER BY NEWID() 
    if (not exists(select 1 FROM'[email protected]_TABLA+ 'WHERE' [email protected]+ '=' [email protected]+ 'AND' [email protected]+ '=' [email protected]+ 'AND' 
[email protected]+ '=' [email protected]+ '))' + 'BEGIN INSERT INTO' [email protected]_TABLA+ 'values (' [email protected]+ ',' [email protected]+ ',' [email protected]+ ',' 
[email protected]+ ')' + 'SET' [email protected]+ '=' [email protected]+ '+1' + 'END END;') 

(コードの完全な試み、それは最初のものです) は、どのように私はこれを可能にすることはできますか?今私は、テーブルを自動生成することができるよしかし、SQL-サーバがこの

> Msg 102, Level 15, State 1, Line 123 Incorrect syntax near '='. Msg 
> 102, Level 15, State 1, Line 124 Incorrect syntax near '='. Msg 102, 
> Level 15, State 1, Line 125 Incorrect syntax near '='. Msg 102, Level 
> 15, State 1, Line 126 Incorrect syntax near '='. Msg 102, Level 15, 
> State 1, Line 126 Incorrect syntax near ','. Msg 102, Level 15, State 
> 1, Line 123 Incorrect syntax near '='. Msg 102, Level 15, State 1, 
> Line 124 Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 
> 125 Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 125 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 126 
> Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 123 
> Incorrect syntax near '='. Msg 102, Level 15, State 1, Line 124 
> Incorrect syntax near '='. 

そして、多くのことを言うので4桁の整数ずに、値の挿入が不可能な多くのそれらはすべてほとんど言います同じ、ほぼ115回私は思います。

+0

これは、動的SQLが必要です。そこにもたくさんの例があります。また、MySQLタグを削除すると、これはまったくSQL Serverのようです。 –

+1

文全体を印刷してみてください。問題を見つけやすくする必要があります。また、スペースを追加することもできます。最後に、これが学生プロジェクトでない限り、あなたのアプローチを再考してください。その場でテーブルを作成するべきではありません。 – under

+0

うん、それは学生のためのプロジェクトだ私は115が多すぎると思う声明全体をコピーしようとする(sqlserverは今別の理由で墜落した) –

答えて

1

何か不足していますか?どの程度

select * into newtable from platos; 

またはあなたのテーブルの作成がOKであれば、あなたはあなたの最初の行を手動で挿入している、選択のインサートを使用します。

insert into newtable (col1, col2, col3, col4) 
select cola, colb, colc, cold from oldtable; 
関連する問題