Sybase常用命令,SQL语句

[size=small]SQL online sample
[url]http://sqlzoo.net/wiki/Main_Page[/url]

[b]查询系统全局变量,日期,日期转换,日期加减[/b]
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())

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

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


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

不是用insert into ... select 语句


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


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


或者

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

[b]重命名字段的名字[/b]
sp_rename 'table1.column1', 'new_column_name'

[b]添加字段[/b]
ALTER TABLE table1 ADD new_column varchar(10) NULL

[b]删除字段[/b]
alter table table1 drop column_to_be_deleted

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

[b]Insert "Space","new line","tab" characters into a field as value[/b]
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 [url]https://awesomesql.wordpress.com/2009/08/10/ascii-character-set-table[/url]. Once this is done, concatenate your string with the character of that particular ASCII value as
[/size]
'my string and'+ char(12)+'someting'


this will be


my string and

something


[size=medium]
[b]Check table Primary Key/Reference Key/Constraints[/b]
sp_helpconstraint tableName


[b]-How to determine a Sybase servers character set and sort order[/b]
sp_helpsort

[b]-how to check permissions on a table in sybase[/b]
sp_helprotect proc_name

[b]-单引号(')转义[/b]
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

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


[b]--Get all tables for the specified column[/b]
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'


[b]--Get all columns for the specified table[/b]
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'


[b]获取Sybase数据库版本信息[/b]
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


[b]who am I?[/b]
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)
[/size]

[b]--To get list of all users and alias in database[/b]
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


[size=medium]
[b]--To get list of all users in database[/b]
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


[b]获取table的lock scheme[/b]
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

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

[b]-Default value[/b]
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

[b]-Identity gap[/b]
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
[b][color=red]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.[/color][/b]

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

[/size]

你可能感兴趣的:(Database)