如何从 JSON 字符串输出中删除 ‘dbo’?

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

How to remove 'dbo' from JSON string output?

问题

我正在尝试通过从多个表中读取数据(单独的选择语句)来生成最终的JSON字符串。

我有一个存储SQL查询的表,如下所示:

EmployeeArchiveTable:

Id     EmployeeId    TableName       SQLQuery
1      1             dbo.Employee    select * from employee where EmployeeID = 1
1      1             dbo.Payroll     select * from Payroll where EmployeeID = 1
1      1             dbo.Leaves      select * from Leaves where EmployeeID = 1
1      1             dbo.Division    select * from Division where EmployeeID = 1

存储过程从上表中读取数据和Employee Id,生成如下的JSON字符串:

期望的输出:

{
  "employeeID 1" : {
     "Employee" : { //员工数据//}, //根据执行查询获取的对象或对象数组
     "Payroll"  : { //工资数据//}, //根据执行查询获取的对象或对象数组
  .
  .
  } 
}

存储过程:

Create Proc [dbo].[getEmployeeJsonByEmployeeId]
  @EmployeeID int
AS
Begin
    declare @json varchar(max) = '';
	
	declare my_cursor CURSOr for
	   select TableName, SQLQuery from EmployeeArchiveTable where employeeID = @employeeID;
	   declare @tableName varchar(50);
	   declare @sqlQuery varchar(max);
	   
	   Fetch next from my_cursor into @tableName,@sqlQuery;
	   
	   while @@FETCH_STATUS = 0
	      Begin
		     select @json += 'Json_Query((' + sqlQuery + ')) as [' + (@tableName) + '], ';
			 fetch next from my_cursor into @tableName, @sqlQuery;
		  End
	   close my_cursor;
	   select @json = 'select ' + substring(@json,1,LEN(@json) - 1) + ' FOR JSON Path, WITHOUT_ARRAY_WRAPPER';
	   print @json;
	   select @json;
End;

输出最终的SQL查询:

select Json_Query((select * from employee where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Employee],
Json_Query((select * from Payroll where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Payroll],
Json_Query((select * from Leaves where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Leaves],
Json_Query((select * from Division where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Division] FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

这将生成JSON输出:

{
  "dbo" : {
     "Employee" : { //员工数据//}, //根据执行查询获取的对象或对象数组
     "Payroll"  : { //工资数据//}, //根据执行查询获取的对象或对象数组
  }
}

我不确定JSON中的"dbo"是从哪里来的,以及如何移除它?

英文:

I am trying to generate the final json string by reading data from multiple tables(Individual Select statement).

I have a table which stores the SQL query like below:

EmployeeArchiveTable:

Id     EmployeeId    TableName       SQLQuery
1      1             dbo.Employee    select * from employee where EmployeeID = 1
1      1             dbo.Payroll     select * from Payroll where EmployeeID = 1
1      1             dbo.Leaves      select * from Leaves where EmployeeID = 1
1      1             dbo.Division    select * from Division where EmployeeID = 1

Stored Procedure to read the data from above table and Employee Id and generate JSON string like below:

Expected Output:

{
  "employeeID 1" : {
     "Employee" : { /employee data/}, //either object or array of object based on whatever we get by executing the query
     "Payroll"  : { /payroll data/}, //either object or array of object based on whatever we get by executing the query
  .
  .
  } 
}

Stored Procedure :

Create Proc [dbo].[getEmployeeJsonByEmployeeId]
  @EmployeeID int
AS
Begin
    declare @json varchar(max) = '';
	
	declare my_cursor CURSOr for
	   select TableName, SQLQuery from EmployeeArchiveTable where employeeID = @employeeID;
	   declare @tableName varchar(50);
	   declare @sqlQuery varchar(max);
	   
	   Fetch next from my_cursor into @tableName,@sqlQuery;
	   
	   while @@FETCH_STATUS = 0
	      Begin
		     select @json += 'Json_Query((' + sqlQuery + ')) as ' + '[' + (@tableName) + '] ' + N', ';
			 fetch next from my_cursor into @tableName, @sqlQuery;
		  End
	   close my_cursor;
	   select @json = 'select ' + substring(@json,1,LEN(@json) - 1) + ' FOR JSON Path, WITHOUT_ARRAY_WRAPPER';
	   print @json;
	   select @json;
End;

Output final SQL Query:

select Json_Query((select * from employee where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Employee],
Json_Query((select * from Payroll where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Payroll],
Json_Query((select * from Leaves where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Leaves],
Json_Query((select * from Division where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Division] FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

This produce JSON output:

{
  "dbo" : {
     "Employee" : { /employee data/}, //either object or array of object based on whatever we get by executing the query
     "Payroll"  : { /payroll data/}, //either object or array of object based on whatever we get by executing the query
  }
}

I am not sure where this 'dbo' is coming from in JSON and how do I remove it?

答案1

得分: 1

'dbo' 来自 EmployeeArchivalTable 中的模式。在存储表名(如 dbo.Employee 等)时,模式名称包含在 JSON 属性名中。如果在 while 循环中添加一行以替换 dbo 部分,这应该有所帮助。

set @tableName = REPLACE(@tableName, 'dbo.', '');

还添加了 quotename 以确保 JSON 中的表名格式正确。在本地测试过,可以得到您所需要的结果:

CREATE PROCEDURE [dbo].[getEmployeeJsonByEmployeeId]
  @EmployeeID int
AS
Begin
    declare @json varchar(max) = '';

    declare my_cursor CURSOR for
       select TableName, SQLQuery from EmployeeArchiveTable where EmployeeID = @employeeID;
       declare @tableName varchar(50);
       declare @sqlQuery varchar(max);

       Fetch next from my_cursor into @tableName, @sqlQuery;

       while @@FETCH_STATUS = 0
          Begin
             -- 从表名中移除模式名称(dbo.)
             set @tableName = REPLACE(@tableName, 'dbo.', '');

             select @json += 'Json_Query((' + @sqlQuery + ' FOR JSON path, INCLUDE_NULL_VALUES)) as ' + QUOTENAME(@tableName) + N', ';
             fetch next from my_cursor into @tableName, @sqlQuery;
          End

       close my_cursor;
       select @json = 'select ' + substring(@json,1,LEN(@json) - 1) + ' FOR JSON Path, WITHOUT_ARRAY_WRAPPER';
       print @json;
       EXEC (@json); -- 执行动态 SQL 以返回 JSON
End;
英文:

'dbo' comes from the schema in in the EmployeeArchivalTable. When the table names are stored(dbo.Employee, etc.) the schema name is included in the JSON property name.
If you add a line in the while loop to replace the dbo part that should help.

````
set @tableName = REPLACE(@tableName, 'dbo.', '');
````

Added quotename too to make sure the table names in the JSON are correctly formatted.

Tested this locally and it gets what you were looking for:

CREATE PROCEDURE [dbo].[getEmployeeJsonByEmployeeId]
  @EmployeeID int
AS
Begin
    declare @json varchar(max) = '';

    declare my_cursor CURSOR for
       select TableName, SQLQuery from EmployeeArchiveTable where EmployeeID = @employeeID;
       declare @tableName varchar(50);
       declare @sqlQuery varchar(max);

       Fetch next from my_cursor into @tableName, @sqlQuery;

       while @@FETCH_STATUS = 0
          Begin
             -- Remove the schema name (dbo.) from the table name
             set @tableName = REPLACE(@tableName, 'dbo.', '');

             select @json += 'Json_Query((' + @sqlQuery + ' FOR JSON path, INCLUDE_NULL_VALUES)) as ' + QUOTENAME(@tableName) + N', ';
             fetch next from my_cursor into @tableName, @sqlQuery;
          End

       close my_cursor;
       select @json = 'select ' + substring(@json,1,LEN(@json) - 1) + ' FOR JSON Path, WITHOUT_ARRAY_WRAPPER';
       print @json;
       EXEC (@json); -- Execute the dynamic SQL to return the JSON
End;

答案2

得分: 1

dbo is in your final query (for example [dbo.Employee])

select Json_Query((select * from employee where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Employee],
Json_Query((select * from Payroll where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Payroll],
Json_Query((select * from Leaves where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Leaves],
Json_Query((select * from Division where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Division] FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

if you want to remove it then replace 'dbo.' in the @tableName. I have modified your code to do so

Create Proc [dbo].[getEmployeeJsonByEmployeeId]
@EmployeeID int
AS
Begin
declare @json varchar(max) = '';

declare my_cursor CURSOr for
   select TableName, SQLQuery from EmployeeArchiveTable where employeeID = @employeeID;
   declare @tableName varchar(50);
   declare @sqlQuery varchar(max);

   Fetch next from my_cursor into @tableName,@sqlQuery;

   while @@FETCH_STATUS = 0
      Begin
         select @json += 'Json_Query((' + sqlQuery + ')) as ' + '[' + (replace(@tableName,'dbo.','') + '] ' + N', ';
         fetch next from my_cursor into @tableName, @sqlQuery;
      End
   close my_cursor;
   select @json = 'select ' + substring(@json,1,LEN(@json) - 1) + ' FOR JSON Path, WITHOUT_ARRAY_WRAPPER';
   print @json;
   select @json;

End;

英文:

dbo is in your final query (for example [dbo.Employee])

select Json_Query((select * from employee where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Employee],
Json_Query((select * from Payroll where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Payroll],
Json_Query((select * from Leaves where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Leaves],
Json_Query((select * from Division where EmployeeID = 1 FOR JSON path, INCLUDE_NULL_VALUES)) as [dbo.Division] FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

if you want to remove it then replace 'dbo.' in the @tableName. I have modified your code to do so

Create Proc [dbo].[getEmployeeJsonByEmployeeId]
@EmployeeID int
AS
Begin
declare @json varchar(max) = '';

declare my_cursor CURSOr for
   select TableName, SQLQuery from EmployeeArchiveTable where employeeID = @employeeID;
   declare @tableName varchar(50);
   declare @sqlQuery varchar(max);

   Fetch next from my_cursor into @tableName,@sqlQuery;

   while @@FETCH_STATUS = 0
      Begin
         select @json += 'Json_Query((' + sqlQuery + ')) as ' + '[' + (replace(@tableName,'dbo.','') + '] ' + N', ';
         fetch next from my_cursor into @tableName, @sqlQuery;
      End
   close my_cursor;
   select @json = 'select ' + substring(@json,1,LEN(@json) - 1) + ' FOR JSON Path, WITHOUT_ARRAY_WRAPPER';
   print @json;
   select @json;

End;

huangapple
  • 本文由 发表于 2023年3月31日 22:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899969.html
匿名

发表评论

匿名网友

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

确定