C#写SQL SERVER2008存储过程

简介: 在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';

 

 

相关实践学习
使用交互方式创建数据表
本次实验主要介绍如何在RDS-SQLServer数据库中使用交互方式创建数据表。
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
相关文章
|
1月前
|
存储 SQL 关系型数据库
【MySQL】探索MySQL存储过程的魔力,初学者的数据库编程秘笈(内含实战SQL脚本)下
【MySQL】探索MySQL存储过程的魔力,初学者的数据库编程秘笈(内含实战SQL脚本)
97 1
|
1月前
|
存储 SQL 关系型数据库
【MySQL】探索MySQL存储过程的魔力,初学者的数据库编程秘笈(内含实战SQL脚本)上
【MySQL】探索MySQL存储过程的魔力,初学者的数据库编程秘笈(内含实战SQL脚本)
32 2
|
1月前
|
存储 SQL
sql_存储过程、函数、分支、循环
sql_存储过程、函数、分支、循环
27 0
|
2月前
|
存储 SQL 安全
数据库SQL Server 9-10 章(存储过程与触发器)
数据库SQL Server 9-10 章(存储过程与触发器)
93 0
|
2月前
|
存储 SQL 数据库
SQL Server——为什么要使用存储过程?不使用是什么样的?
提高数据库执行速度,可能第一次见到这句话的小伙伴们感觉到非常的匪夷所思叭!怎么就提高了它的执行速度捏,从哪方面可以表现出来呢?既然这里要说到的是为什么要使用存储过程,也就是说它的优点是什么。那我们肯定就要对使用和不使用存储过程两方面来进行对比才能看出它的优点对吧。
|
2月前
|
存储 SQL 数据库连接
SQL存储过程
SQL存储过程
29 0
|
2月前
|
存储 SQL 数据库
SQL Sever数据库存储过程
SQL Sever数据库存储过程
|
3月前
|
存储 SQL 数据库
使用SQL语句创建存储过程
使用SQL语句创建存储过程
194 1
|
3月前
|
存储 SQL 关系型数据库
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 2
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 2
159 0
|
3月前
|
存储 SQL NoSQL
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 1
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 1
213 0
相关产品
云迁移中心
推荐文章
更多