首页 > 编程技术 > php

如何得到当前所在的sql的数据库名称

发布时间:2016-11-25 16:44

这里有几个关于SQL SERVER的问题想向您请教(服务器端脚本编程):
(1)如何在SQL SERVER中用SQL语句获得当前的数据库名称?
(2)如何在定义一个全局变量而不是局部变量,因为我在这里定义了
一个循环,在这个循环里有对一个临时表加字段和对临时表插入数据
的操作。但这两个操作必须放在不同的批次里.(需要用GO语句分开)。
这时控制循环的变量参数就不起作用了,因为变量参数只能在一个
批次里起作用,到下一个批次就要重新定义了。真头疼!能不能定义
全局变量,让它在所有批次中都起作用?
这两个问题,其实并不具有一定的普遍性,但是解决这两个问题的 思路和方法 却有一定的 普遍性,所以我专门把这两个问题拿出来 讲一下
(1)首先 这个当前 所在 DataBase 肯定需要用到 系统存储过程 这一点是 十分肯定的,我自然想到了sp_who 执行后,列出了当前所有的 系统进程的 所有信息,我一看 有 Runnable,有 Slepping 肯定应该是 runnable 了,一看 他的 spid 想起来了,有个系统的 全局变量@@SPID,先试一下,sp_who @@spid 哈哈 成功了这个问题的解决之道 是 一定要熟悉 系统的存储过程和系统全局变量,并且要善于观察和思考
(2) 至于 第二个问题,则纯粹是一种 t-sql 编程的思路问题,首先我不赞同用一个规则去判断两个 存储过程,因为这不符和 t-SQL 编程的规范,但是既然问题出来了,就要想办法 解决,我们知道在 两个批次里 是不可能共巷数据的,所以 我想到了 cursor 游标,最终 经过测试,也成功了。

在SQL server 中恢复数据的几种办法:
1.自然就是 backup 的 恢复方法 backup 这种方法是最安全,最可靠的方法操作起来也很简单,只要在 sql server 的 enterprise manager中选择 restore 就可以了。
用T-SQL 也可以完成:
RESTORE DATABASE test FROM DISK = ’c:mssql7ackup est.bak’
当然这是用的 文件恢复,如果是 设备恢复 自然也是可以的。
2.可是有的时候,SQL server 是在我们毫无准备的情况下瘫痪的,有的时候是伴随者 NT 的瘫痪而引起的,(这个时候,豆腐想 墙上 Gates 的画像有仍了几个 西红柿),怎么办呢?这个时候就只有采用 sql server 的 t-sql 中提供的系统存储过程了:
sp_atach_db
在这里举一个简单的例子:
sp_attach_db @dbname = N’pubs’,
@filename1 = N’c:mssql7datapubs.mdf’,
@filename2 = N’c:mssql7datapubs_log.ldf’
这种方法应该说是有很高的成功率的,不过在 实践中 豆腐听说 这样后,数据库成为只读的,那也没有办法了。如果只有 mdf 没有 ldf 文件的话,可以使用 sp_attach_single_file
sp_attach_single_file_db @dbname = ’pubs’, @physname = ’c:mssql7datapubs.mdf’
这个方法本来是 用来 对 sp_deatach 的操作的反操作,不过直接使用也还是可以成功的。
from: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=4&txtCodeId=6518
Connect/Read Remote SQL server Using PWS in win 98
I had to test Microsoft's Personal Webserver (PWS) in win 98 to access Remote SQL server 7.0
installed in a NT server. The clients to win 98 had LAN connections. Easy one , don't vote, have fun with
PWS. In fact I tested my connection with this script, before I created an out of process server demo with
VB.





code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in
compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the
original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or
code's description.

'**************************************
' Name: Connect/Read Remote SQL server U
' sing PWS in win 98
' Description:I had to test Microsoft's
' Personal Webserver (PWS) in win 98 to ac
' cess Remote SQL server 7.0 installed in
' a NT server. The clients to win 98 had L
' AN connections. Easy one , don't vote, h
' ave fun with PWS. In fact I tested my co
' nnection with this script, before I crea
' ted an out of process server demo with V
' B.
' By: Manas Mukherjee
'
在SQL Server中提供了这种恢复方式的存储过程。
一个数据库包括两个文件,mdf数据库文件和ldf日志文件
1.sp_attach_db [@dbname =] 'dbname',[@filename1 =] 'filename_n'
给系统添加一个数据库,在dbname指定数据库名称,filename_n指定数据库的文件和日志文件。比如我有一个ji的库,停止SQL Server服务备份ji_data.mdf,ji_log.ldf,启动SQL server,删除掉这个库,然后再把这两上文件拷到sql server DATA目录中,在Query Analyzer中执行如下语句:
EXEC sp_attach_db @dbname = N'ji',
@filename1 = N'd:mssql7dataji_data.mdf',
@filename2 = N'd:mssql7dataji_log.ldf'
就会把这个库加入到SQL Server Group中.

如果你用的是sql server数据库!你不想用后台操作你可以看看这个
下面是对text的操作你可以看看
1. 写操作(WRITETEXT)
这里一般要用到的函数有TextPtr获得文本字段的指针,和TextVaild检验指针的有效性,@@RowCount判断返回记录的条数。
其基本方法是:用Textptr函数得到指针,判断其有效性,用Writetext写数据
函数说明:Textptr(字段名)。Writetext tablename。Fieldname @textptr(指针) [With Log] data(数据)
例如:
Begin Tran
Declare @Mytextptr VarBinary(16)
Select @mytextptr=textptr(pr_info)
From Pub_Info (updlock)
Where pud_id=’9999’
IF @Mytextptr Is Not Null
Writetext pub_info.pr_info @mytextptr with log 'data’
Commit Tran
2. 读操作
常用函数
PatIndex('%exp%’,var|fieldname。。)
Datalength()
@@TextSize 文本大小
SettextSize N 设置文本大小
ReadText {TableName。FieldName} {@textptr} Offet Size [HoldLock]
例如:
begin tran
Declare @mytextptr Varbinary(16),@Totalsize int,@Readsize int,@lastread int
Set textsize 100
Select @mytextptr=textptr(pr_info), @totalsize=datalength(pr_info)
@lastread=0,
@readsize= case when (textsize<datalength(pr_info) then textsize
eles datalength(pr_info)
end
From Pub_info
Where Pub_id=’1622’
IF @mytextptr Is not Null and @readsize>0
While (@lastread<@totalsize)
ReadText pub_info.pr_info @mytextptr @lastread @readsize holdlock
If (@@error<>0)
Break
Select @lastread=@lastread @readsize
If ((@readsize @lastread)>@totalsize)
Select @readsize=@totalsize-@lastread
End
Commit Tran
3.数据更新UpdateText
更新数据代替了写操作,其基本语法是:
UpdateText Table_Name.Col_Name Text_Ptr Offest(偏移量) Deleted_Length
[With Log] [Inserted_Data|Table_Name.Scr_Column_name Str_Text_Ptr]
说明:
Offest:0说明从开头开始,Null表示你向当前内容追加数据。
Deleted_Length:0表示不删除任何内容,Null表示删除所有内容。
标签:[!--infotagslink--]

您可能感兴趣的文章: