用户定义的函数,内部带有CASE语句

时间:2018-07-11 07:11:22

标签: sql-server

如何在T-SQL标量函数中为以下代码使用case语句? (因为T-SQL标量函数不允许使用动态SQL)

我想在@Table上切换/大小写

ALTER FUNCTION [dbo].[GetRowVersion] (@Id INT, @Table VARCHAR(20)) RETURNS BINARY(8) AS
BEGIN
DECLARE @rowVersion AS BINARY(8)
IF @Table = 'Department' AND @Id = 0 SELECT @rowVersion =  MAX([RowVersion]) FROM Department
IF @Table = 'Department' AND @Id > 0 SELECT @rowVersion =  [RowVersion] FROM Department WHERE Id = @Id
RETURN @rowVersion
END
GO

例如。

开头是案例。

case @Table =“ Region” .....

IF @Table = 'Department' AND @Id = 0 SELECT @rowVersion =  MAX([RowVersion]) FROM Department
IF @Table = 'Department' AND @Id > 0 SELECT @rowVersion =  [RowVersion] FROM Department WHERE Id = @Id
RETURN @rowVersion

case @Table =“部门” ....

IF @Table = 'Department' AND @Id = 0 SELECT @rowVersion =  MAX([RowVersion]) FROM Department
IF @Table = 'Department' AND @Id > 0 SELECT @rowVersion =  [RowVersion] FROM Department WHERE Id = @Id
RETURN @rowVersion

... ... ... 等等 .. 默认情况下。...

RETURN @rowVersion

结束情况。

我无法超越这一点

CREATE FUNCTION [dbo].[GetRowVersion] (@Id INT, @Table VARCHAR(20)) RETURNS BINARY(8) AS
BEGIN
DECLARE @rowVersion AS BINARY(8)
   IF @Table = 'Department'
      BEGIN
         IF @Id = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [Department]
         IF @Id > 0 SELECT @rowVersion = [RowVersion] FROM [Department] WHERE Id = @Id
      END
   ELSE IF @Table = 'Address'
      BEGIN
         IF @Id = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [Address]
         IF @Id > 0 SELECT @rowVersion = [RowVersion] FROM [Address] WHERE Id = @Id
      END
   ELSE IF @Table = 'AddressType'
      BEGIN
         IF @Id = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [AddressType]
         IF @Id > 0 SELECT @rowVersion = [RowVersion] FROM [AddressType] WHERE Id = @Id
      END
   ELSE IF @Table = 'ContactType'
      BEGIN
         IF @Id = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [ContactType]
         IF @Id > 0 SELECT @rowVersion = [RowVersion] FROM [ContactType] WHERE Id = @Id
      END
   RETURN @rowVersion
END
GO

我遇到错误:

信息3701,第11级,州5,第5行 无法删除函数“ dbo.GetRowVersion”,因为该函数不存在或您没有权限。 消息206,级别16,状态2,过程GetRowVersion,第13行[批处理开始第13行] 操作数类型冲突:uniqueidentifier与int不兼容

1 个答案:

答案 0 :(得分:0)

解决方案似乎像这样

CREATE FUNCTION [dbo].[GetRowVersion] (@Key NVARCHAR(36), @Table NVARCHAR(20)) RETURNS BINARY(8) AS
BEGIN
DECLARE @Id INT, @rowVersion AS BINARY(8)
      SELECT @Id = CONVERT(INT, @Key)
   IF @Table = 'Department'
      BEGIN
         IF @Id = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [Department]
         IF @Id > 0 SELECT @rowVersion = [RowVersion] FROM [Department] WHERE Id = @Id
      END
   ELSE IF @Table = 'Address'
      BEGIN
         IF LEN(@Key) = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [Address]
         IF LEN(@Key) > 0 SELECT @rowVersion = [RowVersion] FROM [Address] WHERE Id = @Key
      END
   ELSE IF @Table = 'AddressType'
      BEGIN
         IF @Id = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [AddressType]
         IF @Id > 0 SELECT @rowVersion = [RowVersion] FROM [AddressType] WHERE Id = @Id
      END
   ELSE IF @Table = 'ContactType'
      BEGIN
         IF @Id = 0 SELECT @rowVersion = MAX([RowVersion]) FROM [ContactType]
         IF @Id > 0 SELECT @rowVersion = [RowVersion] FROM [ContactType] WHERE Id = @Id
      END
   RETURN @rowVersion
END
GO