我想知道该声明的错误。
INSERT INTO Table1(Myname,category )
SELECT TOP 1 thenames
FROM tNames
WHERE DateAdded > DATEADD(Day, -10, GETDATE()
ORDER BY NEWID(),@ccategory)
我想从表tnames中选择一个随机值,并将其放在表1中,其中包含我从SP获得的类别值。 我该怎么做?
编辑: 我在MS SQL Server工作。 完整代码:
Create PROCEDURE [dbo].[Names_SP]
@CCategory nvarchar(50)
AS
BEGIN
INSERT INTO Table1(Myname,category )
SELECT TOP 1 thenames
FROM tNames
WHERE DateAdded > DATEADD(Day, -10, GETDATE()
ORDER BY NEWID(),@ccategory)
END
当我拨打SP时,我发送@ccategory。由于表1有2列(第一列是类别),我想从tname表中获取第二个(myname)列值为random(已在10天内添加的名称)。
答案 0 :(得分:2)
您要插入2列(Myname,类别) 但您的选择列表只包含1列! (thenames)
" @ category&#34 ;?至少在你发布的代码中,这个变量永远不会被声明或分配......
也许你应该这样做:
DECLARE @category varchar(20)
SET @category = 'some cat.'
INSERT INTO Table1(Myname,category)
SELECT TOP 1 thenames,@category
FROM tNames
WHERE DateAdded > DATEADD(Day, -10, GETDATE())
ORDER BY NEWID()
答案 1 :(得分:1)
您正在从Select语句中选择One Column,并且在insert语句中指定了两列
INSERT INTO Table1(Myname)
SELECT TOP 1 thenames
FROM tNames
WHERE DateAdded > DATEADD(Day, -10, GETDATE()
ORDER BY NEWID(),@ccategory)
答案 2 :(得分:1)
您必须选择以insert语句编写的确切值。这样的事情: -
INSERT INTO Table1(Myname,category )
SELECT TOP 1 thenames, Category_value
FROM tNames
WHERE DateAdded > DATEADD(Day, -10, GETDATE())
ORDER BY NEWID(),@ccategory
答案 3 :(得分:1)
INSERT INTO Table1(Myname,category )
SELECT TOP 1 thenames,@ccategory
FROM tNames
WHERE DateAdded > DATEADD(Day, -10, GETDATE() ORDER BY NEWID(),@ccategory)
你应该传递相同的参数combinataion of insert和select statement