英文:
Execute stored procedure based on condition to check if a cell value contains data or not
问题
我有一个SQL表(data),它包含3列。
| Id | Firstname | Lastname |
| -- | -- | -- |
|1
| 2 | Abc | Def
我需要根据`Id`列来检查`Firstname`列的值。如果它不包含任何值,我需要在`Firstname`和`Lastname`列中插入值。
我尝试了下面的代码:
Create procedure prodata
@Id nvarchar(50),
@Firstname nvarchar(50),
@Lastname nvarchar(50)
As
Begin
Declare @name as nvarchar(50)
Select @name = Firstname from data where Id = @Id
If(@name is '')
Begin
Update data set Firstname= @Firstname and Lastname=@Lastname
where Id=@Id
End
End
Exec prodata '1','vijay','kumar'
它不起作用。我该怎么做?
英文:
I have SQL table(data) and it contains 3 columns.
Id | Firstname | Lastname |
---|---|---|
1 | ||
2 | Abc | Def |
I have to check the Firstname
column value based on the Id
column. If it doesn't contain any value I need insert values into the Firstname
and Lastname
columns.
I tried the code below:
Create procedure prodata
@Id navarchar(50),
@Firstname nvarchar(50),
@Lastname nvarchar(50)
As
Begin
Declare @name as nvarchar(50)
Select @name = Firstname from data where Id = @Id
If(@name is '')
Begin
Update data set Firstname= @Firstname and Lastname=@Lastname
where Id=@Id
End
End
Exec prodata '1','vijay','kumar'
It is not working. How can I do this?
答案1
得分: 0
请使用以下步骤。我认为这对你有帮助。
-- 创建存储过程 prodata
Create procedure prodata
@Id nvarchar(50),
@Firstname nvarchar(50),
@Lastname nvarchar(50)
As
Begin
Declare @name as nvarchar(50)
Select @name = Firstname
from data
where Id = @Id
If isnull(@name, '') = ''
Begin
Update data set
Firstname = @Firstname, Lastname = @Lastname
where Id = @Id
End
End
英文:
Please use the below procedure. I think it will helpful for you.
Create procedure prodata
@Id nvarchar(50),
@Firstname nvarchar(50),
@Lastname nvarchar(50)
As
Begin
Declare @name as nvarchar(50)
Select @name = Firstname
from data
where Id = @Id
If isnull(@name,'') = ''
Begin
Update data set
Firstname = @Firstname, Lastname = @Lastname
where Id = @Id
End
End
答案2
得分: 0
尝试更改这部分:
If(@name is '')
为:
If (coalesce(rtrim(@name), '') = '')
英文:
Try changing this:
If(@name is '')
To this:
If (coalesce(rtrim(@name), '') = '')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论