I have this table
CREATE TABLE [dbo].[Sale]
(
[SaleId] [int] IDENTITY(1,1) PRIMARY KEY,
[SaleDate] [datetime] NOT NULL DEFAULT GETDATE(),
[UserPhone] char(10) NOT NULL,
[NumberSale] [int] NOT NULL,
)
Then I have this table variable
DECLARE @sale1 TABLE
(
Phone char(10),
GoodId int,
GoodCount int
)
INSERT INTO @sale1
VALUES ('0671112221', 1, 2),
('0671112221', 4, 1),
('0671112221', 13, 2);
I am creating a stored procedure to input data into the table using table variable. But table is underlined.
CREATE PROCEDURE spSalesAndSalesPosInsert
@data table -- here is a mistake
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
INSERT INTO Sale (UserPhone, NumberSale)
SELECT Phone, GoodCount
FROM @data
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
END CATCH;
END;
What am I doing wrong?
The table
keyword is reserved for other scenarios than passing a parameter.
Have look a this example from the documentation that passes a table-valued parameter to a stored procedure by creating a user-defined type .
You need to declare a user-defined type for the table.
CREATE TYPE myTableType AS TABLE
(
Phone char(10), GoodId int, GoodCount int
)
Then you can use it:
declare @sale1 myTableType
insert into @sale1
values
('0671112221', 1, 2),
('0671112221', 4, 1),
('0671112221', 13, 2);
create proc spSalesAndSalesPosInsert
@data myTableType
as
begin
--Etc....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.