`
darrenzhu
  • 浏览: 783818 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Sybase常用命令,SQL语句

阅读更多
SQL online sample
http://sqlzoo.net/wiki/Main_Page

查询系统全局变量,日期,日期转换,日期加减
select @@ERROR
select OBJECT_ID('tableName/procedureName/...')
select getUTCDate()
select convert(datetime, '01/01/01')
select convert(char(26), getdate(), 109)
select convert(char(26), getUTCdate(), 109)
select dateadd(dd, -8, getUTCdate())

复制数据, copy table data
INSERT INTO targetTable SELECT * FROM sourceTable WHERE CLAUSE

INSERT INTO targetTable(column1, column2, ...) SELECT 'a' as column1, column2, ... FROM sourceTable WHERE CLAUSE


复制copy表结构table schema,从查询结构创建表
SELECT * into tbl_allocation_party_history from  tbl_allocation_party where 1=2

不是用insert into ... select 语句


判断表table,存储过程stored procedure是否存在
IF OBJECT_ID('dbo.objName') is not null
    BEGIN
        drop table/procedure dbo.objName
        PRINT '<<<Drop table/procedure dbo.objName>>>'
    END


判断索引是否存在, check whether the index exists?
IF EXISTS (select 1 from sysindexes where id = OBJECT_ID('tableName') and name = 'indexName')
    BEGIN
        drop index tableName.indexName
        PRINT '<<<drop index tableName.indexName>>>'
    END
ELSE
    PRINT '<<<skip drop index tableName.indexName due to not existed>>>'


或者

IF EXISTS (select 1 FROM sysindexes s, sysobjects so WHERE s.name = 'my_index' AND s.id = so.id AND so.type = 'U' AND so.name = 'my_table') BEGIN
  DROP INDEX my_table.my_index
END

重命名字段的名字
sp_rename 'table1.column1', 'new_column_name'

添加字段
ALTER TABLE table1 ADD new_column varchar(10) NULL

删除字段
alter table table1 drop column_to_be_deleted

修改字段的数据类型,是否为空
alter table table1 modify column_name date null

Insert "Space","new line","tab" characters into a field as value
There are times when you need to insert some special characters such as "newline","tab" etc as values into a field. Usually every one tries with '\n' or '\t', in vain. we can do this by using the ASCII value of the character. The steps are very simple. if you want to insert a special character find the ASCII value of that character like '9 for tab', '12 for new line' etc. you can find the lists of characters with its ASCII values at https://awesomesql.wordpress.com/2009/08/10/ascii-character-set-table. Once this is done, concatenate your string with the character of that particular ASCII value as

'my string and'+ char(12)+'someting'


this will be

my string and

something



Check table Primary Key/Reference Key/Constraints
sp_helpconstraint tableName


-How to determine a Sybase servers character set and sort order
sp_helpsort

-how to check permissions on a table in sybase
sp_helprotect proc_name

-单引号(')转义
sybase里面用来转义的符号不是"\",而是单引号,所以
So how do you escape quotes in Sybase? In fact, in Sybase SQL the single quote acts as the escape character.
See below for an example UPDATE statement in both “languages”:
MySQL
UPDATE Animals SET NAME = 'Dog\'s friends' WHERE uid = 12
Sybase
UPDATE Animals SET NAME = 'Dog''s friends' WHERE uid = 12

另外"(" ")" ","是不需要转义的


--Get all tables for the specified column
SQL: select b.name as tablename, a.name as columnname
from syscolumns a join sysobjects b on (a.id = b.id)
where b.type='U' and a.name = 'column'


--Get all columns for the specified table
SQL: select b.name as tablename, a.name as columnname
from syscolumns a join sysobjects b on (a.id = b.id)
where b.type='U' and b.name = 'table'


获取Sybase数据库版本信息
There are two ways to know the about Sybase version,

1) Using this System procedure to get the information about Sybase version
SQL: sp_version

2) Using this command to get Sybase version
SQL: select @@version



who am I?
SQL: SELECT user, user_id()

When you are in a database that you own the answer is dbo (database owner). If you USE someone else's database then you get a user id. (the machine being used to connect to MySQL)


--To get list of all users and alias in database
select 'user:' 'Type', t1.suid, t1.name 'dbuser', t2.name 
from sysusers t1, master..syslogins t2 
where t2.suid=*t1.suid 
union 
select 'alias:' 'Type', t1.suid, ' ' 'dbuser', t2.name 
from sysalternates t1, master..syslogins t2 
where t2.suid=*t1.suid



--To get list of all users in database
SQL: use database
     select name from sysusers where udi<16384
or
SQL: select name from databasename.dbo.sysusers  where udi<16384

--suid is 'server user id ' is reference to sybase login which recorded in master.dbo.syslogins.
--16384 is a magic id number from which group names are recorded in sysusers table. (all with id> 16384 are group names not users)

--There is also what called 'aliases'. To see all sybase login that are aliased to another login in current database:
SQL: select suser_name(suid),suser_name(altsuid) from sysalternates
--Which mean whan anyone named in first column try to access database it will be treated as login from second column.
use master
--Next query will give you sybase database user name and corresponding server login (may be different )
SQL: select l.name,d.name from master.dbo.syslogins l, databasename.dbo.sysusers d where l.suid=d.suid


获取table的lock scheme
1) select lockscheme('tableName')
2) SELECT sysobj.* FROM (
select name, lockscheme(name) lockscheme, type from sysobjects
) as sysobj
where sysobj.type='U' and sysobj.lockscheme != 'datarows'

3) select convert(varchar(30),name) as OBJECT_NAME,
       "LOCKING_SCHEME" = case (sysstat2 & 57344)
         when 8192 then "all pages"
         when 16384 then "datapages"
         when 32768 then "datarows"
       end
from sysobjects where type in ("U") order by name

SQL for changing the locking scheme for a table
alter table table_name lock {allpages | datapages | datarows}

-Default value
Actually the default value of a column is a constraint for the table, you can use sp_helpconstraint TABLENAME to check.
Firstly we should use create default and drop default to achieve our goal.
To alter a default you need to use replace rather than modify:
alter table downloads replace is_completed default 0
If you need to change the data type or the null/not null then you should use
alter table t modify c

-Identity gap
Managing identity gaps in tables
The IDENTITY column contains a unique ID number, generated by Adaptive Server, for each row in a table. Because of the way the server generates ID numbers by default, you may occasionally have large gaps in the ID numbers. The identity_gap parameter gives you control over ID numbers, and potential gaps in them, for a specific table.

By default, Adaptive Server allocates a block of ID numbers in memory instead of writing each ID number to disk as it is needed, which requires more processing time. The server writes the highest number of each block to the table’s object allocation map (OAM) page. This number is used as the starting point for the next block after the currently allocated block of numbers is used or “burned.” The other numbers of the block are held in memory, but are not saved to disk. Numbers are considered burned when they are allocated to memory, then deleted from memory either because they were assigned to a row, or because they were erased from memory due to some abnormal occurrence such as a system failure.

Allocating a block of ID numbers improves performance by reducing contention for the table. However, if the server fails or is shut down with no wait before all the ID numbers are assigned, the unused numbers are burned. When the server is running again, it starts numbering with the next block of numbers based on the highest number of the previous block that the server wrote to disk. Depending on how many allocated numbers were assigned to rows before the failure, you may have a large gap in the ID numbers.

Identity gaps can also result from dumping and loading an active database. When dumping, database objects are saved to the OAM page. If an object is currently being used, the maximum used identity value is not in the OAM page and, therefore, is not dumped.

Setting the table-specific identity gap
Set the table-specific identity gap when you create a table using either create table or select into.

This statement creates a table named mytable with an identity column:

create table mytable (IdNum numeric(12,0) identity)
with identity_gap = 10
The identity gap is set to 10, which means ID numbers are allocated in memory in blocks of ten. If the server fails or is shut down with no wait, the maximum gap between the last ID number assigned to a row and the next ID number assigned to a row is ten numbers.

If you are creating a table in a select into statement from a table that has a specific identity gap setting, the new table does not inherit the identity gap setting from the parent table. Instead, the new table uses the identity burning set factor setting. To give the new table a specific identity_gap setting, specify the identity gap in the select into statement. You can give the new table an identity gap that is the same as or different from the parent table.

For example, to create a new table (newtable) from the existing table (mytable) with an identity gap:

select IdNum into newtable
with identity_gap = 20
from mytable

分享到:
评论

相关推荐

    sybase库中导出全部表的oracle、mysql和sybase的建表语句

    sybase库中导出全部表的oracle、mysql和sybase格式的建表语句

    sql语句生成器+支持各大数据库+说明书

    SQL语句生成器的特色 支持几乎所有类型的数据库, 包括小型(桌面)数据库:Fox DBF、Microsoft Execl、Text、Borland Paradox、 中型数据库:Microsoft Access 大型数据库:Microsoft SQL Server、Sybase、Oracle ...

    SQL语句生成及分析器

    无论多么复杂的语句,都能分析出来(包括SQL各子句中嵌套的SQL语句) 5、数据库视图定义和重建 6、支持将SQL查询语句,替换为插入(Insert into)和更新(Update)语句 7、附属工具内嵌入Delphi IDE(支持Delphi 5和...

    Sql语句和数据库教程

    以及SQL语法,SQL语句。表结构、视图、函数、索引等,常见数据库问题的处理。 适合人群:需要系统学习数据库和SQL的,想提升技能的,想解决数据库问题的 能学到什么:了解几种数据库的使用和底层原理,以及SQL语句的...

    Mysql、oracle、Sybase数据库两列合并成一列的sql语句

    Mysql、oracle、Sybase数据库两列合并成一列的sql语句

    SQL语句生成及分析器(中文绿色)

    1、支持绝大部分数据库,包括 大型数据库Oracle,Sybase(包括SQL AnyWhere),DB2,MS_SQL 中型数据库MS_Access,MySQL ... 10.3 SQL语句关键词高亮度显示 10.4 简单SQL查询语句转换为Delete,Update,Insert语句

    sybase sql 优化器

    即是之前的LECCO SQL Expert,不仅能够找到最佳的SQL语句,它所提供的“边做边学式训练”还能够教开发人员和数据库管理员如何写出性能最好的SQL语句。它的“SQL比较器”可以标明源SQL和待选SQL间的不同之处。

    sybase数据库命令指南

    sybase数据库命令指南 构件块 介绍了 Transact-SQL 的各个“部件”:数据类型、内置 函数、全局变量、表达式、标识符、保留字和 SQLSTATE 错误。 为了学会使用 Transact-SQL,您需要首先理解这些构件块的功 能,...

    sql语句生成与分析器

    大型数据库Oracle(包括Oracle Lite),Sybase(包括SQL AnyWhere),DB2,MS_SQL 中型数据库MS_Access,MySQL 桌面型数据库Paradox,DBF系列数据库,MS_Execl,Text 其他支持SQL 92标准的数据库

    Sql经典练习题库(附答案)

    如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。 美国国家标准局(ANSI)与国际...

    Sybase IQ 16.0中文版本 参考:语句和选项

    本手册适用于需要 SAP Sybase IQ SQL 语句和数据库选项的相关参考资料的 SAP® Sybase® IQ 用户。 有关 SAP Sybase IQ 其它方面的参考资料(包括语言元素、数据类型、函数、系统过 程和系统表),请参见《参考:...

    SQL语句自动生成器简体中文版

    支持几乎所有类型的数据库, 包括小型(桌面)数据库:Fox DBF、Microsoft Execl、Text、Borland Paradox、 中型数据库:Microsoft Access 大型数据库:Microsoft SQL Server、Sybase、Oracle &lt;br&gt;...

    SQL语句生成及分析器 v2.0

    支持绝大部分数据库,包括大型数据库Oracle,Sybase(包括SQL AnyWhere),DB2,MS_SQL中型数据库MS_Access,MySQL桌面型数据库Paradox,DBF系列数据库,MS_Execl,Text其他支持SQL 92标准的数据库

    SQL语句自动生成工具

    支持几乎所有类型的数据库, 包括小型(桌面)数据库:Fox DBF、Microsoft Execl、Text、Borland Paradox、 中型数据库:Microsoft Access 大型数据库:Microsoft SQL Server、Sybase、Oracle &lt;br&gt;...

    SQL server的sql语句知识体系框架

    对sql语句的学习,复习都有帮助。 SQL 概述 SQL,一般发音为 sequel,SQL 的全称 Structured Query Language),SQL 用来和数据库打交道,完成和数据库的通信,SQL 是一套标准。但是每一个数据库都有自己的特性别的...

    Repair For Sybase Sql Anywhere

    可以直接打开SyBase数据库,可查看数据库结构,但不能写SQL语句,也没有连接语句

    sql语句生成与分析器.rar

    11.3 SQL语句关键词高亮度显示 11.4 简单SQL查询语句转换为Delete,Update,Insert语句 11.5 复制为字符串(支持对Java、C#、Delphi、VB、PowerBuilder开发语言的支持) 11.6 灵活的拖放功能 11.7 在线版本...

    恢复Sybase SQL Anywhere数据库的好方法

    SQLAnywhere的SQL语句基本上和SybaseAdaptiveServer一致;而且功能也相当强;PowerBuilder5.0以上的版本都自带一个SQLAnywhere。因此,对使用笔记本或单机的开发人员来说,选择SQLAnywhere作为数据库来开发应用程序...

    数据库SQL语句循序渐进教程

    SQL语句通常用于完成一些数据库的操作任务,比如在数据库中更新数据,或者从数据库中检索数据。使用SQL的常见关系数据库管理系统有:Oracle、 Sybase、 Microsoft SQL Server、 Access、 Ingres等等。虽然绝大多数的...

    SybaseIQ12.7实用程序指南

    本书提供有关与 Sybase IQ 搭配使用的实用程序的参考资料。实用程 序是指那些可直接从操作系统调用的命令。还有其它书籍提供了有 关如何执行特定任务的详细背景信息。您可使用此参考书来查找可 用的语法、参数和选项...

Global site tag (gtag.js) - Google Analytics