在 SQL 服务器中创建一个接受参数 (user_name) 和 returns a table 的函数
Creates a function in SQL Server that accepts a parameter (user_name) and returns a table
我想将以下代码输入一个函数,该函数将接受一个参数 (User_name) 和 return table。
代码如下:
DECLARE @_l_Table_Data nvarchar(max)
DECLARE @user_name nvarchar(max)
SET @user_name= (SELECT USER_NAME FROM sysaid_user WHERE user_name = @user_name)
SET @_l_Table_Data = (SELECT user_cust_json FROM sysaid_user WHERE user_name = 'CONSIST-SYS\alonb')
SELECT *
FROM OPENJSON(@_l_Table_Data)
WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
);
SQL 服务器中的典型函数是标量,返回单个值。如果您需要 rows/columns,那么您可以使用 table 值函数。
由于您没有提供测试数据,我无法对此进行测试,但您可以尝试以下操作:
CREATE FUNCTION dbo.FetchCustom (
@user_name NVARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN (
SELECT
jsn.ci_id, jsn.amount, jsn.user_n
FROM sysaid_user AS usr
CROSS APPLY (
SELECT *
FROM OPENJSON ( usr.user_cust_json )
WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
)
) AS jsn
WHERE
usr.[USER_NAME] = @user_name
)
GO
使用该函数的基本调用是:
SELECT * FROM dbo.FetchCustom ( 'CONSIST-SYS\alonb' );
另外,NVARCHAR(MAX)
是怎么回事?您真的有需要使用 MAX 的用户名吗?我严重怀疑。一个好的做法是将您的大小限制为它们可以包含的最大值(例如,NVARCHAR(50)
)。
更新:
如果 usr.user_cust_json 包含另一个实际上是动态的密钥 例如:{"data": {"4251":{"ci_id":"4251", “金额”:“-50”,“user_name”:“AssetStorage”},“4289”:{“ci_id”:“4289”,“金额”:“-8”,user_name":"AssetStorage"},"4280":{"ci_id":"4280","amount":"-1","user_name":"AssetStorage"}} } 如何在这个函数中显示这个键?这需要什么改变?
要在结果集中包含键值,您可以像这样修改 TVF:
CREATE FUNCTION dbo.FetchCustom (
@user_name NVARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN (
SELECT
jsn.[key], jsn.ci_id, jsn.amount, jsn.user_n
FROM sysaid_user AS usr
CROSS APPLY (
SELECT
[key], ci_id, amount, user_n
FROM OPENJSON ( usr.user_cust_json, '$.data' ) AS k
CROSS APPLY (
SELECT
ci_id, amount, user_n
FROM OPENJSON ( k.[value] ) WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
)
) AS d
) AS jsn
WHERE
usr.[USER_NAME] = @user_name
)
GO
如果您要 运行 在 SSMS 中执行以下操作,您可以看到基于您提供的 JSON 的基础结果集。
DECLARE @json VARCHAR(1000) = '{"data": {"4251":{"ci_id":"4251","amount":"-50","user_name":"AssetStorage"} ,"4289":{"ci_id":"4289","amount":"-8","user_name":"AssetStorage"} ,"4280":{"ci_id":"4280","amount":"-1","user_name":"AssetStorage"}}}';
SELECT
[key], ci_id, amount, user_n
FROM OPENJSON ( @json, '$.data' ) AS k
CROSS APPLY (
SELECT
ci_id, amount, user_n
FROM OPENJSON ( k.[value] ) WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
)
) AS d;
Returns
+------+-------+--------+--------------+
| key | ci_id | amount | user_n |
+------+-------+--------+--------------+
| 4251 | 4251 | -50 | AssetStorage |
| 4289 | 4289 | -8 | AssetStorage |
| 4280 | 4280 | -1 | AssetStorage |
+------+-------+--------+--------------+
我想将以下代码输入一个函数,该函数将接受一个参数 (User_name) 和 return table。
代码如下:
DECLARE @_l_Table_Data nvarchar(max)
DECLARE @user_name nvarchar(max)
SET @user_name= (SELECT USER_NAME FROM sysaid_user WHERE user_name = @user_name)
SET @_l_Table_Data = (SELECT user_cust_json FROM sysaid_user WHERE user_name = 'CONSIST-SYS\alonb')
SELECT *
FROM OPENJSON(@_l_Table_Data)
WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
);
SQL 服务器中的典型函数是标量,返回单个值。如果您需要 rows/columns,那么您可以使用 table 值函数。
由于您没有提供测试数据,我无法对此进行测试,但您可以尝试以下操作:
CREATE FUNCTION dbo.FetchCustom (
@user_name NVARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN (
SELECT
jsn.ci_id, jsn.amount, jsn.user_n
FROM sysaid_user AS usr
CROSS APPLY (
SELECT *
FROM OPENJSON ( usr.user_cust_json )
WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
)
) AS jsn
WHERE
usr.[USER_NAME] = @user_name
)
GO
使用该函数的基本调用是:
SELECT * FROM dbo.FetchCustom ( 'CONSIST-SYS\alonb' );
另外,NVARCHAR(MAX)
是怎么回事?您真的有需要使用 MAX 的用户名吗?我严重怀疑。一个好的做法是将您的大小限制为它们可以包含的最大值(例如,NVARCHAR(50)
)。
更新:
如果 usr.user_cust_json 包含另一个实际上是动态的密钥 例如:{"data": {"4251":{"ci_id":"4251", “金额”:“-50”,“user_name”:“AssetStorage”},“4289”:{“ci_id”:“4289”,“金额”:“-8”,user_name":"AssetStorage"},"4280":{"ci_id":"4280","amount":"-1","user_name":"AssetStorage"}} } 如何在这个函数中显示这个键?这需要什么改变?
要在结果集中包含键值,您可以像这样修改 TVF:
CREATE FUNCTION dbo.FetchCustom (
@user_name NVARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN (
SELECT
jsn.[key], jsn.ci_id, jsn.amount, jsn.user_n
FROM sysaid_user AS usr
CROSS APPLY (
SELECT
[key], ci_id, amount, user_n
FROM OPENJSON ( usr.user_cust_json, '$.data' ) AS k
CROSS APPLY (
SELECT
ci_id, amount, user_n
FROM OPENJSON ( k.[value] ) WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
)
) AS d
) AS jsn
WHERE
usr.[USER_NAME] = @user_name
)
GO
如果您要 运行 在 SSMS 中执行以下操作,您可以看到基于您提供的 JSON 的基础结果集。
DECLARE @json VARCHAR(1000) = '{"data": {"4251":{"ci_id":"4251","amount":"-50","user_name":"AssetStorage"} ,"4289":{"ci_id":"4289","amount":"-8","user_name":"AssetStorage"} ,"4280":{"ci_id":"4280","amount":"-1","user_name":"AssetStorage"}}}';
SELECT
[key], ci_id, amount, user_n
FROM OPENJSON ( @json, '$.data' ) AS k
CROSS APPLY (
SELECT
ci_id, amount, user_n
FROM OPENJSON ( k.[value] ) WITH (
ci_id NVARCHAR(max) '$.ci_id' ,
amount NVARCHAR(max) '$.amount',
user_n NVARCHAR(max) '$.user_name'
)
) AS d;
Returns
+------+-------+--------+--------------+
| key | ci_id | amount | user_n |
+------+-------+--------+--------------+
| 4251 | 4251 | -50 | AssetStorage |
| 4289 | 4289 | -8 | AssetStorage |
| 4280 | 4280 | -1 | AssetStorage |
+------+-------+--------+--------------+