我有一个存储过程,用于将数据插入SQL Server 2008中的表。
这是我的存储过程:
ALTER PROCEDURE [dbo].[RegisterOnline_Insert]
--@OrderType int,
@SiteID as varchar(50),
@CustName as varchar(100),
@Address as varchar(255),
@Phone as varchar(20),
@HP as varchar(20),
@Email as varchar(100),
@Description as varchar(500),
@IsValid as bit
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @House as numeric(10,0)
DECLARE @Fibernode as varchar(50)
DECLARE @ftax as numeric(5,0)
DECLARE @Demographics2 AS VARCHAR(50)
DECLARE @PostalCode as varchar(10)
DECLARE @InputBy as varchar(50)
DECLARE @addr as varchar(max)
DECLARE @addrid as int
SET @InputBy = '[Website]'
SET @addr = dbo.udf_StripHTML(@Address)
SELECT @PostalCode = postal_code,
@ftax = ftax,
@Fibernode = fibernode,
@Demographics2 = demographics2,
@addrid = Address_id
FROM dbo.TEMP_HOUSE_INFO2_SITE
WHERE site_id = @SiteID
SET NOCOUNT ON;
INSERT INTO tbMynetOrder(
OrderType,
SITE_ID ,
AddressID,
House,
Cust,
CustName,
Address,
PostalCode,
Fibernode,
PhoneOld,
Phone,
HPOld,
HP,
Email,
ftax,
BoD,
ContactPerson,
IDCard,
PaymentMethod,
Description,
SalesCode,
SchedDate,
SchedTimeID,
Complete,
InstallationCost,
Source,
Covered,
CCIssueBy,
CCType,
CCNo,
CCExpireDate,
CCHolderName,
CCHolderPhone,
CCFrequency,
CCPromoService,
CCAmount,
Demographics2,
SCA,
BlackList,
InputBy,
InputDate
)
VALUES(
1, -- Order Type
@SiteID,
ISNULL(@addrid,0), -- Site ID
null, -- House
null, -- Cust
UPPER(@CustName), -- Customer Name
@addr, -- Address
@PostalCode, -- PostalCode
@Fibernode, -- FiberNode
@Phone, -- PhoneOld
null, -- Phone
@HP, -- HP Old
@HP, -- HP
@Email, -- Email
@ftax, -- ftax
null, -- BOD
null, -- Contact Person
null, -- ID Card
null, -- Payment Method
@Description, -- Description
null, -- Sales Code
null, --SchedDate
null, -- SchedTimeID
null, -- Complete
null, -- InstallationCost
3, -- Source , 3 = Website
@IsValid, -- Covered
null, -- CCIssueBy
null, -- CCType
null, -- CCNo
null, -- CCExpireDate
null, -- CCHolderName
null, -- CCHolderPhone
null, -- Frequency
null, -- CCPromo
null, -- CCAmount
@Demographics2, -- Demographics2
null, -- SCA
null, -- Blacklist
@InputBy, -- Inputby
GETDATE() -- Input Date
)
END
我的@SiteID
参数值存在问题,当它处理获取某个值时,它有值并给出结果:
SELECT @PostalCode = postal_code,
@ftax = ftax,
@Fibernode = fibernode,
@Demographics2 = demographics2,
@addrid = Address_id
FROM dbo.TEMP_HOUSE_INFO2_SITE
WHERE site_id = @SiteID
但是当插入到表中时,该值变为null,为什么会发生?我的插入语句有问题吗?
答案 0 :(得分:2)
试试这个
当 SET NOCOUNT ON; 然后SP没有返回值你的 command.executenonqeury 要求结果其他方面它回滚操作 所以插入语句后写选择scope_identity()返回上次插入 PK
插入后
如果你写select scope_identity()
select scope_identity()
现在您的处理就像这样
ALTER PROCEDURE [dbo].[RegisterOnline_Insert]
--@OrderType int,
@SiteID as varchar(50),
@CustName as varchar(100),
@Address as varchar(255),
@Phone as varchar(20),
@HP as varchar(20),
@Email as varchar(100),
@Description as varchar(500),
@IsValid as bit
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @House as numeric(10,0)
DECLARE @Fibernode as varchar(50)
DECLARE @ftax as numeric(5,0)
DECLARE @Demographics2 AS VARCHAR(50)
DECLARE @PostalCode as varchar(10)
DECLARE @InputBy as varchar(50)
DECLARE @addr as varchar(max)
DECLARE @addrid as int
SET @InputBy = '[Website]'
SET @addr = dbo.udf_StripHTML(@Address)
SELECT @PostalCode = postal_code,
@ftax = ftax,
@Fibernode = fibernode,
@Demographics2 = demographics2,
@addrid = Address_id
FROM dbo.TEMP_HOUSE_INFO2_SITE
WHERE site_id = @SiteID
SET NOCOUNT ON;
INSERT INTO tbMynetOrder(
OrderType,
SITE_ID ,
AddressID,
House,
Cust,
CustName,
Address,
PostalCode,
Fibernode,
PhoneOld,
Phone,
HPOld,
HP,
Email,
ftax,
BoD,
ContactPerson,
IDCard,
PaymentMethod,
Description,
SalesCode,
SchedDate,
SchedTimeID,
Complete,
InstallationCost,
Source,
Covered,
CCIssueBy,
CCType,
CCNo,
CCExpireDate,
CCHolderName,
CCHolderPhone,
CCFrequency,
CCPromoService,
CCAmount,
Demographics2,
SCA,
BlackList,
InputBy,
InputDate
)
VALUES(
1, -- Order Type
@SiteID,
ISNULL(@addrid,0), -- Site ID
null, -- House
null, -- Cust
UPPER(@CustName), -- Customer Name
@addr, -- Address
@PostalCode, -- PostalCode
@Fibernode, -- FiberNode
@Phone, -- PhoneOld
null, -- Phone
@HP, -- HP Old
@HP, -- HP
@Email, -- Email
@ftax, -- ftax
null, -- BOD
null, -- Contact Person
null, -- ID Card
null, -- Payment Method
@Description, -- Description
null, -- Sales Code
null, --SchedDate
null, -- SchedTimeID
null, -- Complete
null, -- InstallationCost
3, -- Source , 3 = Website
@IsValid, -- Covered
null, -- CCIssueBy
null, -- CCType
null, -- CCNo
null, -- CCExpireDate
null, -- CCHolderName
null, -- CCHolderPhone
null, -- Frequency
null, -- CCPromo
null, -- CCAmount
@Demographics2, -- Demographics2
null, -- SCA
null, -- Blacklist
@InputBy, -- Inputby
GETDATE() -- Input Date
)
select scope_identity()
END