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 @NOMBRE_TABLA=@MES
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 + '=' +@PlatoP+ 'from' +@Platos+ 'where' +@tipo+ '=' +@A+@entrada1+@A+ 'ORDER BY NEWID()
Select TOP 1' + @xSopa + '=' +@PlatoP+ 'from' +@Platos+ 'where' +@tipo+ '=' +@A+@sopa1+@A+ 'ORDER BY NEWID()
Select TOP 1' + @xPlato + '=' +@PlatoP+ 'from' +@Platos+ 'where' +@tipo+ '=' +@A+@plato1+@A+ 'ORDER BY NEWID()
if (not exists(select 1 FROM'+@NOMBRE_TABLA+ 'WHERE' +@entrada1+ '=' +@xEntrada+ 'AND' +@Sopa1+ '=' +@xSopa+ 'AND'
+@PLATO1+ '=' +@xPlato+ '))' + 'BEGIN INSERT INTO' +@NOMBRE_TABLA+ 'values (' +@nd+ ',' +@xEntrada+ ',' +@xSopa+ ','
+@xPlato+ ')' + 'SET' +@nd+ '=' +@nd+ '+1' + 'END END;')
end
END
END
这是我的尝试: 1)自动创建一个表,只需输入“Exec inselt'varchar','一个4位的int'” 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 @nd=@nd+1
END
END;
将该代码转换为:
EXECUTE(
'Select TOP 1' + @xEntrada + '=' +@PlatoP+ 'from' +@Platos+ 'where' +@tipo+ '=' +@A+@entrada1+@A+ 'ORDER BY NEWID()
Select TOP 1' + @xSopa + '=' +@PlatoP+ 'from' +@Platos+ 'where' +@tipo+ '=' +@A+@sopa1+@A+ 'ORDER BY NEWID()
Select TOP 1' + @xPlato + '=' +@PlatoP+ 'from' +@Platos+ 'where' +@tipo+ '=' +@A+@plato1+@A+ 'ORDER BY NEWID()
if (not exists(select 1 FROM'+@NOMBRE_TABLA+ 'WHERE' +@entrada1+ '=' +@xEntrada+ 'AND' +@Sopa1+ '=' +@xSopa+ 'AND'
+@PLATO1+ '=' +@xPlato+ '))' + 'BEGIN INSERT INTO' +@NOMBRE_TABLA+ 'values (' +@nd+ ',' +@xEntrada+ ',' +@xSopa+ ','
+@xPlato+ ')' + 'SET' +@nd+ '=' +@nd+ '+1' + 'END END;')
(代码的完整尝试,这是第一个) 我怎样才能做到这一点?现在我只能自动生成一个表但没有4位数的int,因为sql-server说这个值不可能插入值
> 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 '='.
还有更多,但他们都说几乎相同,我认为差不多115次。
答案 0 :(得分:1)
我错过了什么吗? <怎么样
select * into newtable from platos;
或者,如果您的表创建正常,并且您已手动插入第一行,请使用insert select:
insert into newtable (col1, col2, col3, col4)
select cola, colb, colc, cold from oldtable;