MSSQL 程序集使用方法

1.C# 写一个程序

1.1新建一个项目【类库【.Net FrameWork】

1.2编写代码

删除

namespace ApiSQLClass

{

}

代码如下:【具体调用API模式根据具体编写】

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Server;


    public class ApiSQLHelper
    { 
    [SqlFunction]
    public static string CallAPI(string apiMethod, string apiURL, string apiPMS)
    {
        return apiPMS;
    }
    }

2.数据库设定

2.1数据库设定

sql 复制代码
Use master 
exec sp_configure 'show advanced options', '1';
go

reconfigure;
go

exec sp_configure 'clr enabled', '1'
go

reconfigure;
exec sp_configure 'show advanced options', '1';
go 
Use [DB] --DB 具体为需要使用的数据库

ALTER DATABASE [DB]  SET TRUSTWORTHY on;
go

2.2程序集设定

1.选择需要设定数据库后按下步骤进行

3.SQL使用

3.1函数代码

sql 复制代码
create function [dbo].[fn_CallAPI]
(
	@apiMethod nvarchar(max)		--Post/Get
	, @apiURL nvarchar(max)		--API地址		
	 
	, @apiPMS nvarchar(max)		--JSON格式的参数
)
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS 
EXTERNAL NAME [ApiSQLClass].[ApiSQLHelper].[CallAPI]
 

3.2测试效果

sql 复制代码
declare @apiPMS nvarchar(max);
--set @pms='{"Username":"Test","Password":"Test"}'   --参数
set @apiPMS = (select 'admin' as 'User_id', '123' as 'PSW' for json path, without_array_wrapper);
select [dbo].[fn_CallAPI]( 'POST', 'apits',  @apiPMS)
 

结果集:{"User_id":"admin","PSW":"123"}

4.总结

4.1:需要删除:namespace,否则在创建函数时候报错

4.2:新建;类库【.Net FrameWork】,使用其他类型都未成功

4.3:.Net语句修改重新生成编译,需要把SQL 程序集删除重新设定才是修改后效果。

相关推荐
今天AI了吗2 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
Csvn3 小时前
📊 SQL 入门 Day 22:视图与物化视图
后端·sql
瀚高PG实验室12 小时前
SQL优化案例:存储过程、复杂SQL拆成简单SQL提升整体性能
数据库·sql·postgresql·瀚高数据库
Code额12 小时前
基于 SQLAlchemy 2.x,面向 MySQL 数据库的从入门到精通实战教程(2)
数据库·sql·mysql·adb·ai
wear工程师15 小时前
MySQL 索引条件下推是什么?Using index condition 并不是覆盖索引
sql·mysql
smilejingwei1 天前
Trae+SQLazy 实践 SQL 国产化移植:Oracle => 达梦
数据库·sql·oracle
茉莉玫瑰花茶1 天前
SQL 调优 [ 1 ]
数据库·sql
Databend1 天前
从传统分区到微分区,Snowflake 与 Databend 如何减少数据扫描
大数据·数据库·sql
不会叫的狼1 天前
sql基础
sql
三8442 天前
MySQL 文件读写函数详解:从 CTF 实战到 LOAD_FILE、INTO OUTFILE、LOAD DATA INFILE 应用
数据库·sql