C#写SQL SERVER2008存储过程

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: 在C#中,选择"新建项目"->"数据库"->"SQL Server"->"Visual C# SQL CLR 数据库" 在项目中,单击右键选择"添加"->"存储过程",代码如下(SalesCrossTabs.

在C#中,选择"新建项目"->"数据库"->"SQL Server"->"Visual C# SQL CLR 数据库"

在项目中,单击右键选择"添加"->"存储过程",代码如下(SalesCrossTabs.cs):

 1 using System;
 2 using System.Data;
 3 using System.Data.SqlClient;
 4 using System.Data.SqlTypes;
 5 using Microsoft.SqlServer.Server;
 6 
 7 
 8 public partial class StoredProcedures
 9 {
10     [Microsoft.SqlServer.Server.SqlProcedure]
11     public static void GetSalesPerTerritoryByMonth(SqlDateTime StartDate,SqlDateTime EndDate)
12     {
13         // 在此处放置代码
14         SqlCommand command = new SqlCommand();
15         command.Connection = new SqlConnection("Context connection=true");
16         command.Connection.Open();
17 
18         string sql = "select DISTINCT Convert(char(7),h.OrderDate,120) As YYYY_MM " +
19             "FROM Sales.SalesOrderHeader h " +
20             "WHERE h.OrderDate BETWEEN @StartDate AND @EndDate "+
21             "ORDER BY YYYY_MM";
22         command.CommandText = sql.ToString();
23 
24         SqlParameter param = command.Parameters.Add("@StartDate", SqlDbType.DateTime);
25         param.Value = StartDate;
26         param = command.Parameters.Add("@EndDate", SqlDbType.DateTime);
27         param.Value = EndDate;
28 
29         SqlDataReader reader = command.ExecuteReader();
30 
31         System.Text.StringBuilder yearsMonths=new System.Text.StringBuilder();
32         while (reader.Read())
33         {
34             yearsMonths.Append("["+(string)reader["YYYY_MM"]+"],");
35         }
36         //close the reder
37         //MessageBox.Show(yearsMonths.ToString());
38         reader.Close();
39 
40         if (yearsMonths.Length > 0)
41         {
42             yearsMonths.Remove(yearsMonths.Length - 1, 1);
43         }
44         else
45         {
46             command.CommandText =
47                 "RAISEERROR('No date present for the input date range.',16,1)";
48             try
49             {
50                 SqlContext.Pipe.ExecuteAndSend(command);
51             }
52             catch
53             {
54                 return;
55             }
56         }
57 
58         //Define the cross-tab query
59         sql =
60             "Select TerritoryId," +
61                 yearsMonths.ToString() +
62             "From " +
63             "(" +
64                 "SELECT  " +
65                     "TerritoryId, " +
66                     "CONVERT(char(7),h.OrderDate,120) AS YYYY_MM, " +
67                     "d.LineTotal " +
68                 "From Sales.SalesOrderHeader h " +
69                 "JOIN Sales.SalesOrderDetail d " +
70                     " ON h.SalesOrderID=d.SalesOrderID " +
71                 " WHERE h.OrderDate between @StartDate AND @EndDate " +
72             ") p " +
73             "PIVOT " +
74             "( " +
75                 "SUM (LineTotal) " +
76                 "FOR YYYY_MM IN " +
77                 "( " +
78                     yearsMonths.ToString() +
79                 ") " +
80             ") As pvt " +
81             "ORDER BY TerritoryId";
82 
83         //set the CommandText
84         command.CommandText = sql.ToString();
85 
86         //have the caller execute the cross-tab query
87         SqlContext.Pipe.ExecuteAndSend(command);
88 
89         //close the connection
90         command.Connection.Close();
91     }
92 };

生成并部署此存储过程,在Test.sql中写测试语句
USE AdventureWorks; 
Exec GetSalesPerTerritoryByMonth @StartDate='20070501',@EndDate='20070701';

 

 

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
相关文章
|
13天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
1月前
|
存储 SQL Go
sqlserver存储过程
sqlserver存储过程
19 0
|
1月前
|
存储 SQL 数据库
sqlserver中常用的几个存储过程
sqlserver中常用的几个存储过程
48 0
|
1月前
|
SQL 数据库 数据安全/隐私保护
Sql Server数据库Sa密码如何修改
Sql Server数据库Sa密码如何修改
|
1月前
|
SQL 数据库 C#
C# .NET面试系列十一:数据库SQL查询(附建表语句)
#### 第1题 用一条 SQL 语句 查询出每门课都大于80 分的学生姓名 建表语句: ```sql create table tableA ( name varchar(10), kecheng varchar(10), fenshu int(11) ) DEFAULT CHARSET = 'utf8'; ``` 插入数据 ```sql insert into tableA values ('张三', '语文', 81); insert into tableA values ('张三', '数学', 75); insert into tableA values ('李四',
65 2
C# .NET面试系列十一:数据库SQL查询(附建表语句)
|
23天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
17 0
|
1月前
|
存储 SQL 数据库
sql serve存储过程
sql serve存储过程
14 0
|
13天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
78 6
|
1天前
|
SQL 关系型数据库 MySQL
:“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versi
:“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versi
6 0
|
8天前
|
SQL 安全 网络安全
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
19 0