mssql server

1.安装后无法登陆

服务器类型选择:数据库引擎。

服务器名称写上:计算机名\安装SQL Server时自己创建的实例名。

身份验证选择:SQL Server 身份验证。


2. 使用sql语句添加ndf文件

alter database ClassNorthwind add file( NAME = N'ClassNorthwind_Data2', FILENAME = N'F:\ClassNorthwind_Data2.ndf' , SIZE = 30720KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )

3.通过bak文件还原数据库



查询唯一记录,可以使用临时表


select distinct * from (select supplierID,categoryid from products)t


ADO连接数据库

2008R2,如果连接不上,这里要看下端口配置,如果为空,修改为1433后重启服务


_bstr_t strConnect =  "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=foodline;Data Source=127.0.0.1,1433";


触发器

在插入触发器中查看插入的内容

if (object_id('ygb_safety', 'tr') is not null)
    drop trigger ygb_safety
go
create trigger ygb_safety
on YGB
    for insert --插入触发
as
-- select * from inserted
    --定义变量
    declare  @name varchar(20);
    select @name = leadername from inserted;
    print @name  

go


----触发器
--if (object_id('tgr_parking', 'TR') is not null)
--    drop trigger tgr_parking
--go
--create trigger tgr_parking
--on parking
--    for insert --插入触发
--as
--    --定义变量
--    declare @num int;    
--    select @num = COUNT(1) from ParkLot where HaveCar = '否';
--    if @num < 1
-- print '无空车位';
-- rollback tran; --回退
--go


带参数存储过程

-- exec GetParkHistory '粤C5698'


--create proc GetEmployee
--@name varchar(24)
--as
--begin
--select * from ParkAdmin where Name = @name
--end


-- exec GetEmployee '吴明'


使用ADO添加元素的时候,如果有自增的属性,则使用如下方式进行添加

cstrSql.Format("insert into Parking(EId, CarPlate, ParkNo, BeginTime) values('%s', '%s','%s','%s')",
"002",
m_cstrPlateNo,
cstrParkLot,
strEndTime.c_str());


复杂点的存储过程

存储过程返回值只能是整型,要返回字符串,请用output参数

--获取客户过去一个日期的入住服务员姓名
if object_id('GetReserName') is not null
drop proc GetReserName
go
create proc GetReserName
@cuname varchar(32),
@date varchar(10),
@staffname varchar(32) output
as
begin
declare @cuid int;
declare @staffid int;
select @cuid = CustomerID from Customer where Name = @cuname;
if @cuid is null
return -1;
select @staffid = StaffID from Reservation where Reservationtime = CONVERT(datetime,@date,101) and CustomerID = @cuid;
if @staffid is null
return -2;
select @staffname = Name from Staff where StaffID = @staffid
return 1
end
go
declare @staffname varchar(32)
declare @res int
exec @res = GetReserName 'john', "11-07-2011", @staffname output
select @res
if (0 = @res)
select @staffname




猜你喜欢

转载自blog.csdn.net/xiaoxiaoyu85/article/details/53055094