Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of comments on Sql statements in Mysql

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

< @OriginalSize / 16) AND (@Counter < 50000)) BEGIN -- update INSERT DummyTransVALUES ('Fill Log')DELETE DummyTrans SELECT @Counter = @Counter + 1 END EXEC (@TruncLog) ENDSELECT 'Final Size of ' + db_name() + ' LOG is ' + CONVERT(VARCHAR(30),size) + ' 8K pages or ' + CONVERT(VARCHAR(30),(size*8/1024)) + 'MB' FROM sysfiles WHERE name = @LogicalFileNameDROP TABLE DummyTransSET NOCOUNTOFF8、说明:更改某个表exec sp_changeobjectowner'tablename','dbo'9、存储更改全部表CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch@OldOwneras NVARCHAR(128),@NewOwneras NVARCHAR(128)ASDECLARE @Name as NVARCHAR(128)DECLARE @Owner as NVARCHAR(128)DECLARE @OwnerName as NVARCHAR(128)DECLARE curObjectCURSOR FORselect 'Name' =name, 'Owner' = user_name(uid)from sysobjectswhere user_name(uid)=@OldOwnerorder by nameOPEN curObjectFETCH NEXT FROM curObjectINTO @Name, @OwnerWHILE(@@FETCH_STATUS=0)BEGIN if @Owner=@OldOwnerbegin set @OwnerName = @OldOwner + '.' + rtrim(@Name) exec sp_changeobjectowner @OwnerName, @NewOwnerend-- select @name,@NewOwner,@OldOwnerFETCH NEXT FROM curObjectINTO @Name, @OwnerENDclose curObjectdeallocate curObjectGO10、SQL SERVER中直接循环写入数据declare @iintset @i=1while @i=010:获取某一个表的所有字段select name from syscolumnswhere id=object_id('表名')select name from syscolumnswhere idin (select idfrom sysobjectswhere type = 'u' and name ='表名')两种方式的效果相同11:查看与某一个表相关的视图、存储过程、函数select a.*from sysobjects a, syscomments b where a.id = b.id and b.textlike '%表名%'12:查看当前数据库中所有存储过程select name as 存储过程名称from sysobjectswhere xtype='P'13:查询用户创建的所有数据库select *from master..sysdatabases D where sidnot in(select sidfrom master..sysloginswhere name='sa')或者select dbid,name AS DB_NAMEfrom master..sysdatabaseswhere sid 0x0114:查询某一个表的字段和数据类型select column_name,data_typefrom information_schema.columnswhere table_name = '表名'15:不同服务器数据库之间的数据操作--创建链接服务器exec sp_addlinkedserver 'ITSV ',' ','SQLOLEDB ','远程服务器名或ip地址 'exec sp_addlinkedsrvlogin 'ITSV ','false ',null,'用户名 ','密码 '--查询示例select *from ITSV.数据库名.dbo.表名--导入示例select *into 表from ITSV.数据库名.dbo.表名--以后不再使用时删除链接服务器exec sp_dropserver 'ITSV ','droplogins '--连接远程/局域网数据(openrowset/openquery/opendatasource)--1、openrowset--查询示例select *from openrowset('SQLOLEDB ','sql服务器名 ';'用户名 ';'密码 ',数据库名.dbo.表名)--生成本地表select *into 表from openrowset('SQLOLEDB ','sql服务器名 ';'用户名 ';'密码 ',数据库名.dbo.表名)--把本地表导入远程表insert openrowset('SQLOLEDB ','sql服务器名 ';'用户名 ';'密码 ',数据库名.dbo.表名)select *from 本地表--更新本地表update bset b.列A=a.列A from openrowset('SQLOLEDB ','sql服务器名 ';'用户名 ';'密码 ',数据库名.dbo.表名)as ainner join 本地表 bon a.column1=b.column1--openquery用法需要创建一个连接--首先创建一个连接创建链接服务器exec sp_addlinkedserver 'ITSV ',' ','SQLOLEDB ','远程服务器名或ip地址 '--查询select *FROM openquery(ITSV, 'SELECT * FROM 数据库.dbo.表名 ')--把本地表导入远程表insert openquery(ITSV, 'SELECT * FROM 数据库.dbo.表名 ')select *from 本地表--更新本地表update bset b.列B=a.列BFROM openquery(ITSV, 'SELECT * FROM 数据库.dbo.表名 ')as ainner join 本地表 b on a.列A=b.列A--3、opendatasource/openrowsetSELECT *FROM opendatasource('SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta--把本地表导入远程表insert opendatasource('SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名select *from 本地表SQL Server基本函数SQL Server基本函数1.字符串函数 长度与分析用1,datalength(Char_expr) 返回字符串包含字符数,但不包含后面的空格2,substring(expression,start,length) 取子串,字符串的下标是从"1",start为起始位置,length为字符串长度,实际应用中以len(expression)取得其长度3,right(char_expr,int_expr) 返回字符串右边第int_expr个字符,还用left于之相反4,isnull( check_expression , replacement_value )如果check_expression為空,則返回replacement_value的值,不為空,就返回check_expression字符操作类5,Sp_addtype 自定義數據類型例如:EXEC sp_addtype birthday, datetime, 'NULL'6,set nocount {on|off}使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息。如果存储过程中包含的一些语句并不返回许多实际的数据,则该设置由于大量减少了网络流量,因此可显著提高性能。SETNOCOUNT 设置是在执行或运行时设置,而不是在分析时设置。SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。SET NOCOUNT 为 OFF 时,返回计数常识在SQL查询中:from后最多可以跟多少张表或视图:256在SQL语句中出现Order by,查询时,先排序,后取在SQL中,一个字段的最大容量是8000,而对于nvarchar(4000),由于nvarchar是Unicode码。SQLServer2000同步复制技术实现步骤一、 预备工作1.发布服务器,订阅服务器都创建一个同名的windows用户,并设置相同的密码,做为发布快照文件夹的有效访问用户--管理工具--计算机管理--用户和组--右键用户--新建用户--建立一个隶属于administrator组的登陆windows的用户(SynUser)2.在发布服务器上,新建一个共享目录,做为发布的快照文件的存放目录,操作:我的电脑--D:\ 新建一个目录,名为: PUB--右键这个新建的目录--属性--共享--选择"共享该文件夹"--通过"权限"按纽来设置具体的用户权限,保证第一步中创建的用户(SynUser) 具有对该文件夹的所有权限--确定3.设置SQL代理(SQLSERVERAGENT)服务的启动用户(发布/订阅服务器均做此设置)开始--程序--管理工具--服务--右键SQLSERVERAGENT--属性--登陆--选择"此账户"--输入或者选择第一步中创建的windows登录用户名(SynUser)--"密码"中输入该用户的密码4.设置SQL Server身份验证模式,解决连接时的权限问题(发布/订阅服务器均做此设置)企业管理器--右键SQL实例--属性--安全性--身份验证--选择"SQL Server 和 Windows"--确定5.在发布服务器和订阅服务器上互相注册企业管理器--右键SQL Server组--新建SQL Server注册...--下一步--可用的服务器中,输入你要注册的远程服务器名 --添加--下一步--连接使用,选择第二个"SQL Server身份验证"--下一步--输入用户名和密码(SynUser)--下一步--选择SQL Server组,也可以创建一个新组--下一步--完成6.对于只能用IP,不能用计算机名的,为其注册服务器别名(此步在实施中没用到) (在连接端配置,比如,在订阅服务器上配置的话,服务器名称中输入的是发布服务器的IP)开始--程序--Microsoft SQL Server--客户端网络实用工具--别名--添加--网络库选择"tcp/ip"--服务器别名输入SQL服务器名--连接参数--服务器名称中输入SQL服务器ip地址--如果你修改了SQL的端口,取消选择"动态决定端口",并输入对应的端口号二、 正式配置1、配置发布服务器打开企业管理器,在发布服务器(B、C、D)上执行以下步骤:(1) 从[工具]下拉菜单的[复制]子菜单中选择[配置发布、订阅服务器和分发]出现配置发布和分发向导(2) [下一步] 选择分发服务器 可以选择把发布服务器自己作为分发服务器或者其他sql的服务器(选择自己)(3) [下一步] 设置快照文件夹采用默认\\servername\Pub(4) [下一步] 自定义配置可以选择:是,让我设置分发数据库属性启用发布服务器或设置发布设置否,使用下列默认设置(推荐)(5) [下一步] 设置分发数据库名称和位置 采用默认值(6) [下一步] 启用发布服务器 选择作为发布的服务器(7) [下一步] 选择需要发布的数据库和发布类型(8) [下一步] 选择注册订阅服务器(9) [下一步] 完成配置2、创建出版物发布服务器B、C、D上(1)从[工具]菜单的[复制]子菜单中选择[创建和管理发布]命令(2)选择要创建出版物的数据库,然后单击[创建发布](3)在[创建发布向导]的提示对话框中单击[下一步]系统就会弹出一个对话框。对话框上的内容是复制的三个类型。我们现在选第一个也就是默认的快照发布(其他两个大家可以去看看帮助)(4)单击[下一步]系统要求指定可以订阅该发布的数据库服务器类型,SQLSERVER允许在不同的数据库如 orACLE或ACCESS之间进行数据复制。但是在这里我们选择运行"SQL SERVER 2000"的数据库服务器(5)单击[下一步]系统就弹出一个定义文章的对话框也就是选择要出版的表注意: 如果前面选择了事务发布 则再这一步中只能选择带有主键的表(6)选择发布名称和描述(7)自定义发布属性 向导提供的选择:是 我将自定义数据筛选,启用匿名订阅和或其他自定义属性否 根据指定方式创建发布 (建议采用自定义的方式)(8)[下一步] 选择筛选发布的方式(9)[下一步] 可以选择是否允许匿名订阅1)如果选择署名订阅,则需要在发布服务器上添加订阅服务器方法: [工具]->

[copy]-> [configure release, Properties of subscriber and distribution]-> [subscriber] add a prompt that would otherwise appear when requesting a subscription at the subscriber: changing the publication does not allow anonymous subscription if anonymous subscription is still required, use the following solution [Enterprise Manager]-> [copy]-> [publication]-> [Properties]-> [subscription option] Select allow anonymous pull subscription 2) if anonymous subscription is selected The above prompt will not appear when configuring the subscriber (10) [next] set Snapshot Agent schedule (11) [next] complete the configuration when the publication is created, the database of the publication will be created into a shared database with data srv1. The library name.. author has a field: id,name,phone,srv2. Library name.. author has fields: id,name,telphone,adress requirements: srv1. If the record is added to the library name... author, srv1. The name of the library... author record adds srv1. If the phone field of the library name.. author is updated, then srv1. The library name.. author corresponds to the field telphone update-- * /-- general processing steps-- 1. Create a connection server on srv1 to operate srv2 in srv1 to synchronize the sql instance name or ip'exec sp_addlinkedsrvlogin'srv2','false',null,' user name of exec sp_addlinkedserver'srv2','','SQLOLEDB','srv2', 'password' go--2. On both srv1 and srv2 computers, start msdtc (distributed transaction service) and set it to start automatically. My computer-- Control Panel-- Administrative tools-- Services-- right-click the Distributed Transaction Coordinator-- property-- start-- and set the startup type to start go-- automatically and then create a job to call the synchronization stored procedure above regularly. Just call the enterprise manager-- manage-- SQL Server agent-- right-click job-- create a job-- enter the job name in the "General" item. -- "step" item-- New-- enter step name in "step name"-- Select "Transact-SQL script (TSQL)" in "Type"-- "Database" Select the database in which the command is executed-- enter the statement to be executed in "Command": exec pairing processing-- OK-- "scheduling" item-- New schedule-- enter schedule name in "name"-- selected "scheduling Type" Choose your homework execution schedule-- if you select "repeat"-- click "change" to set your schedule and start the SQL Agent service. And set to start automatically, otherwise your homework will not be executed. Setting method: my computer-- Control Panel-- Administrative tools-- Services-- right-click the SQLSERVERAGENT-- property-- Startup Type-- Select "Auto-start"-- OK.-- 3. Method 2 to achieve synchronization, timing synchronization-- create the following synchronization stored procedure create proc pumped processing in srv1-update the modified data update bset name=i.name,telphone=i.telphonefrom srv2. Library name. Dbo.author bjorn author iwhere b.id=i.idand (b.name i.name or b.telphone i.telphone)-insert the new data insert srv2. Library name .dbo.author (id,name,telphone) select id,name,telphonefrom author iwhere not exists (select * from srv2. Library name. Dbo.authorwhere id=i.id)-deletes deleted data (if necessary) delete bfrom srv2. Library name. Dbo.author bwhere not exists (select * from authorwhere id=b.id) go thank you for reading this article carefully. I hope the article "sample Analysis of Sql sentences in Mysql" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report