暂时先做个记录,以后再将各个部分完善起来
目录
3.第一条语句是等同于in的方法,主要是因为在C#中用@countryCode传输值得时候,将'hk','pk'的数值当做了字符串来处理,
1.sqlserver中一种查父节点id的方法。
方法不难,其实就是 递归调出所有的父节点,但是把不懂这个原理怎么实现的,看着临时表mt可以用在union all 里本身还是有点懵的,暂时先记录一下这个方法
with mt as
(select t.ST_ID , t.ST_Name , t.ST_PID
from AWeb_SupplyType t where ST_ID =10051
union all
select t.ST_ID , t.ST_Name , t.ST_PID from AWeb_SupplyType t , mt
where t.ST_ID = mt.ST_PID )
select ST_ID , ST_Name , ST_PID from mt where ST_PID=0
2.sqlserver合并多条数据
FOR XML PATH 具体来讲是将查询结果集以XML形式
基本用法是输出时行节点可由row变为MyHobby ,列名由字段名变为别名
SELECT CountryCode as 'Code', ContientType as 'Type' FROM PR_ServerCountry FOR XML PATH ('MyHobby')
将其用于表中,可将xml的格式转换为列表格式path(")就是讲行节点row置为''
select stuff((select ','+CountryCode from PR_ServerCountry for xml path('')),1,1,'') as name
3.第一条语句是等同于in的方法,主要是因为在C#中用@countryCode传输值得时候,将'hk','pk'的数值当做了字符串来处理,
select * from PR_ServerCountry where charindex(rtrim(CountryCode), 'pk,hk')>0
select * from PR_ServerCountry where CountryCode in ('hk','pk')
4.排序中为null的
此处的排序要记住是要进行两次,第一次改变值,第二次才是排序(?)
select UserID,UserNames
from UserInfo
order by case when Id is null then 1 else 0 end asc,Id asc
5.查询数据库中的所有索引
SELECT CASE
WHEN t.[type] = 'U' THEN
'表'
WHEN t.[type] = 'V' THEN
'视图'
END AS '类型',
SCHEMA_NAME(t.schema_id) + '.' + t.[name] AS '(表/视图)名称',
i.[name] AS 索引名称,
SUBSTRING(column_names, 1, LEN(column_names) - 1) AS '列名',
CASE
WHEN i.[type] = 1 THEN
'聚集索引'
WHEN i.[type] = 2 THEN
'非聚集索引'
WHEN i.[type] = 3 THEN
'XML索引'
WHEN i.[type] = 4 THEN
'空间索引'
WHEN i.[type] = 5 THEN
'聚簇列存储索引'
WHEN i.[type] = 6 THEN
'非聚集列存储索引'
WHEN i.[type] = 7 THEN
'非聚集哈希索引'
END AS '索引类型',
CASE
WHEN i.is_unique = 1 THEN
'唯一'
ELSE
'不唯一'
END AS '索引是否唯一'
FROM sys.objects t
INNER JOIN sys.indexes i
ON t.object_id = i.object_id
CROSS APPLY
(
SELECT col.[name] + ', '
FROM sys.index_columns ic
INNER JOIN sys.columns col
ON ic.object_id = col.object_id
AND ic.column_id = col.column_id
WHERE ic.object_id = t.object_id
AND ic.index_id = i.index_id
ORDER BY col.column_id
FOR XML PATH('')
) D(column_names)
WHERE t.is_ms_shipped <> 1
AND index_id > 0
ORDER BY i.[name];
6.创建游标
整理下游标的知识,每次都要面向百度编程
-- 定义游标 cur_income 为游标的名称.
DECLARE cur_income CURSOR LOCAL FOR
SELECT PC_ID,PC_Name,PC_Code FROM AWeb_ProductClass;
--声明游标临时变量
DECLARE
@id_income NVARCHAR(50),
@name_income NVARCHAR(50),
@code_income NVARCHAR(50);
OPEN cur_income --打开游标
FETCH NEXT FROM cur_income INTO @id_income,@name_income,@code_income
--将游标指向的数据赋值给声明的变量 ,游标指向结果中第一行
--判断游标的状态
--0 fetch语句成功
--1 fetch语句失败或此行不在结果集中
--2 被提取的行不存在
WHILE @@FETCH_STATUS = 0 --语句成功
BEGIN
// 业务场景
DECLARE @a int=0
DECLARE @b int=0;
if @code_income='a,'
begin
set @a=1;
set @b=2;
end
else begin
set @a=1;
set @b=2;
end
--插入到汇总表
INSERT INTO c(a,b,c,d,e)
VALUES (@id_income,@name_income,@Amount,@a,@b)
--执行下一行,直到完成结果集中所有行
FETCH NEXT FROM cur_income INTO @id_income,@name_income,@code_income
END
CLOSE cur_income--关闭游标
DEALLOCATE cur_income-- 释放游标
7.查全部的存储和视图过程
select a.name,a.[type],b.[definition] from sys.all_objects a,sys.sql_modules b
where a.is_ms_shipped=0 and a.object_id = b.object_id and a.[type] in ('P','V','AF')
order by a.[name] asc
8.创建一个存储过程
// 存储名
ALTER PROCEDURE b (
@returnStr nvarchar(MAX) = null OUTPUT) // 输出returnStr值
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
--更新续费状态
update a set b='1' ;
set @returnStr ='更新成功。';
END TRY
BEGIN CATCH
set @returnStr ='更新失败。'
return;
END CATCH
END
10新增字段和注释
ALTER TABLE 表名 ADD 字段名 类型 默认值 是否为空;
EXEC sp_addextendedproperty N'MS_Description', N'注释内容', N'SCHEMA', N'dbo',N'TABLE', N'表名', N'COLUMN', N'字段名';