执行存储过程,基于条件检查单元格的值是否包含数据。

huangapple go评论63阅读模式
英文:

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), '') = '')

huangapple
  • 本文由 发表于 2023年4月6日 23:26:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定