我的想法如果在存储过程的插入方法中提供的值为Null,我想插入默认值。假设我想为列Lastname插入默认值,如果提供的值为null
我SP
Alter PROCEDURE [dbo].[Any]
@ID int=0,
@Name = varchar(max),
@Lastname = varchar(max),
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRAN
BEGIN TRY
BEGIN
INSERT INTO dbo.ABC
( ID,
Name,
Address)
Values
(
@ID,
@Name,
@Lastname)
我尝试过的代码我尝试使用这段代码,显示在case语句
中有错误INSERT INTO dbo.ABC
( ID,
Name,
case when Address = NULL then 'N/A' else
Address)
您可以将您的插入语表述为INSERT INTO ... SELECT
:
INSERT INTO dbo.ABC (ID, Name, Address)
SELECT ID, Name, COALESCE(Address, 'N/A');
或者,您也可以在表定义中给Address
列一个默认值N/A
。