远程备份sql数据库到本机
条件:
拥有sa权限
开启 xp_cmdshell
方法:
开启/关闭 xp_cmdshell 必须在 master 库执行
EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
--启用xp_cmdshell
EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'xp_cmdshell', 0
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
--关闭xp_cmdshell
本地开启一个共享文件夹 e.g. document
执行语句 --以下在要本分库执行
backup database dbname to disk= 'd:\backdbname.bak ' with init,password= ''
exec xp_cmdshell 'net use z: \\192.168.36.38\document " " /user:des-dev3\rfq '
exec xp_cmdshell 'copy d:\backdbname.bak z:\'
exec xp_cmdshell 'net use z: /delete '
原理 通过sql指令在服务器建立一个网络磁盘 指向 本机共享文件夹 服务器备份数据库到服务器一目录
通过sql 指令copy 备份文件到网络磁盘
删除网络磁盘
相关文档:
Select * from tableName
exec('select * from tableName')
exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错 ......
sql语句,取出表A中的第31条到40条记录(表A以自动增长的ID做主键,注意ID可能是不连续的)
-->select top 10 * from a where id not in (select top 30 id from a order by id) order by id
查询前十条记录,但条件是:ID不在前三十条的ID里面
-->select top 10 * from (select top 40 ......
在列出表中所有字段名的时候,用到了这样一个SQL函数:object_id
这里我将其作用与用法列出来,好让大家明白:
OBJECT_ID:
返回数据库对象标识号。
语法
OBJECT_ID ( 'object' )
参数
'object'
要使用的对象。object 的数据类型为 char 或 nchar。如果 object 的数据类型是 char,那么隐性将其转换成 ncha ......
将Excel文件数据库导入SQL Server的三种方案
//方案一: 通过OleDB方式获取Excel文件的数据,然后通过DataSet中转到SQL Server
openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel files(*.xls)|*.xls";
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
FileInfo ......
--基于时间SQL函数--
getdate() --返回当前系统日期和时间。
DateAdd --在向指定日期加上一段时间的基础上,返回新的 datetime 值。
DATEADD ( datepart , number, date )
--例:向当天的时间增加5天
select dateadd(dd,5,getdate())
datediff --返回跨两个指定日期的日期和时间边界数。]
---例如
& ......