Показ дописів із міткою ms sql. Показати всі дописи
Показ дописів із міткою ms sql. Показати всі дописи

четвер, 13 січня 2011 р.

Пример курсора


declare @Login nvarchar(255)
declare @Password nvarchar(255)
declare @i int

DECLARE curs CURSOR FOR
SELECT [Login], [Password]
FROM [UsersToCreate]

OPEN curs

FETCH NEXT FROM curs INTO @Login, @Password

set @i=1

WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
exec('CREATE LOGIN ['+@Login+'] WITH PASSWORD = '''+@Password+'''')
exec('CREATE USER ['+@Login+'] FOR LOGIN ['+@Login+'] WITH DEFAULT_SCHEMA = my_sc')
exec('EXEC sp_addrolemember ''my_role'', ['+@Login+']')
END TRY
BEGIN CATCH
print cast(@i as char(3))+ ') login: ' + @Login + ' - error:' + ERROR_MESSAGE()
set @i= @i+1
END CATCH;
FETCH NEXT FROM curs INTO @Login, @Password
END

CLOSE curs
DEALLOCATE curs

пʼятниця, 16 липня 2010 р.

MSSQL paging + sorting + filtering


CREATE PROCEDURE [TestTable_GetPagedAndSortedAndFiltered]
(
@startRowIndex int,
@maximumRows int,
@order nvarchar(50),
@ID int = null,
@Name nvarchar(50) = null,
@Size int = null,
@From datetime = null,
@To datetime = null
)
AS
BEGIN

declare @lastKey int
declare @lastAsc sql_variant
declare @lastDesc sql_variant

set rowcount @startRowIndex

select
@lastAsc = OrderAsc,
@lastDesc = OrderDesc,
@lastKey = OrderKey
from
(
select *,
ID as OrderKey,
case @order
when 'ID' then cast(ID as sql_variant)
when 'Name' then cast(Name as sql_variant)
when 'Size' then cast(Size as sql_variant)
when 'Created' then cast(Created as sql_variant)
else ID
END as OrderAsc,
case @order
when 'ID DESC' then cast(ID as sql_variant)
when 'Name DESC' then cast(Name as sql_variant)
when 'Size DESC' then cast(Size as sql_variant)
when 'Created DESC' then cast(Created as sql_variant)
END as OrderDesc
from TestTable
where
((@ID is null) or (@ID = ID))
and
((@Name is null) or (Name like @Name))
and
((@Size is null) or (@Size = Size))
and
((@From is null) or (@From < Created))
and
((@To is null) or (dateadd(day,1,@To) > Created))
) as T
order by OrderAsc,OrderDesc desc,OrderKey

set rowcount @maximumRows

select *
from
(
select *,
ID as OrderKey,
case @order
when 'ID' then cast(ID as sql_variant)
when 'Name' then cast(Name as sql_variant)
when 'Size' then cast(Size as sql_variant)
when 'Created' then cast(Created as sql_variant)
else ID
END as OrderAsc,
case @order
when 'ID DESC' then cast(ID as sql_variant)
when 'Name DESC' then cast(Name as sql_variant)
when 'Size DESC' then cast(Size as sql_variant)
when 'Created DESC' then cast(Created as sql_variant)
END as OrderDesc
from TestTable
where
((@ID is null) or (@ID = ID))
and
((@Name is null) or (Name like @Name))
and
((@Size is null) or (@Size = Size))
and
((@From is null) or (@From < Created))
and
((@To is null) or (dateadd(day,1,@To) > Created))

)as T
where
(
@lastAsc is null
and
@lastDesc is null
and
@lastKey is null
)
or
(
@lastAsc is not null
and
(
OrderAsc > @lastAsc
or
OrderAsc = @lastAsc
and
OrderKey >= @lastKey
)
)
or
(
@lastDesc is not null
and
(
OrderDesc < @lastDesc
or
OrderDesc = @lastDesc
and
OrderKey >= @lastKey
)
)
order by OrderAsc,OrderDesc desc,OrderKey

set rowcount 0

END

MSSQL sort and filter procedure

Припустимо в нас є табличка:

CREATE TABLE [TestTable](
[ID] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Size] [int] NULL,
[Created] [datetime] NULL
)

Для сортування і фільтрації по всіх полях можна використати процедуру:

CREATE PROCEDURE [TestTable_GetPagedAndSorted]
(
@startRowIndex int,
@maximumRows int,
@order nvarchar(50)
)
AS
BEGIN

declare @lastKey int
declare @lastAsc sql_variant
declare @lastDesc sql_variant

set rowcount @startRowIndex

select
@lastAsc = OrderAsc,
@lastDesc = OrderDesc,
@lastKey = OrderKey
from
(
select *,
ID as OrderKey,
case @order
when 'ID' then cast(ID as sql_variant)
when 'Name' then cast(Name as sql_variant)
when 'Size' then cast(Size as sql_variant)
when 'Created' then cast(Created as sql_variant)
else ID
END as OrderAsc,
case @order
when 'ID DESC' then cast(ID as sql_variant)
when 'Name DESC' then cast(Name as sql_variant)
when 'Size DESC' then cast(Size as sql_variant)
when 'Created DESC' then cast(Created as sql_variant)
END as OrderDesc
from TestTable
) as T
order by OrderAsc,OrderDesc desc,OrderKey

set rowcount @maximumRows

select *
from
(
select *,
ID as OrderKey,
case @order
when 'ID' then cast(ID as sql_variant)
when 'Name' then cast(Name as sql_variant)
when 'Size' then cast(Size as sql_variant)
when 'Created' then cast(Created as sql_variant)
else ID
END as OrderAsc,
case @order
when 'ID DESC' then cast(ID as sql_variant)
when 'Name DESC' then cast(Name as sql_variant)
when 'Size DESC' then cast(Size as sql_variant)
when 'Created DESC' then cast(Created as sql_variant)
END as OrderDesc
from TestTable
)as T
where
(
@lastAsc is null
and
@lastDesc is null
and
@lastKey is null
)
or
(
@lastAsc is not null
and
(
OrderAsc > @lastAsc
or
OrderAsc = @lastAsc
and
OrderKey > @lastKey
)
)
or
(
@lastDesc is not null
and
(
OrderDesc < @lastDesc
or
OrderDesc = @lastDesc
and
OrderKey > @lastKey
)
)
order by OrderAsc,OrderDesc desc,OrderKey

set rowcount 0

END

MSSQL ORDER BY CASE

Припустимо в нас є табличка:

CREATE TABLE [TestTable](
[ID] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Size] [int] NULL,
[Created] [datetime] NULL
)


Сортувати її за значення певного параметра можна так:

declare @order nvarchar(50)
set @order = 'Created DESC'

select *
from TestTable
order by

case @order
when 'ID' then cast(ID as sql_variant)
when 'Name' then cast(Name as sql_variant)
when 'Size' then cast(Size as sql_variant)
when 'Created' then cast(Created as sql_variant)
END,
case @order
when 'ID DESC' then cast(ID as sql_variant)
when 'Name DESC' then cast(Name as sql_variant)
when 'Size DESC' then cast(Size as sql_variant)
when 'Created DESC' then cast(Created as sql_variant)
END desc

вівторок, 4 травня 2010 р.

MS SQL sysobjects table

SELECT * FROM sysobjects WHERE xtype = <type>

<type>:
C: Check constraint
D: Default constraint
F: Foreign Key constraint
L: Log
P: Stored procedure
PK: Primary Key constraint
RF: Replication Filter stored procedure
S: System table
TR: Trigger
U: User table
UQ: Unique constraint
V: View
X: Extended stored procedure

1. Get all tables:

SELECT [name]
FROM sysobjects
WHERE xtype = 'U'
order by [name]

2. Get all columns by table:

SELECT syscolumns.name as [name],
systypes.name as [datatype],
syscolumns.length as [length]
FROM sysobjects
JOIN syscolumns ON sysobjects.id = syscolumns.id
JOIN systypes ON syscolumns.xtype=systypes.xtype
WHERE sysobjects.xtype='U' and sysobjects.name = <table name>
ORDER BY syscolumns.colid

субота, 1 травня 2010 р.

Raise error from SQL code

RAISERROR('error message', 16, 1)

середа, 23 грудня 2009 р.

not null -> true and null -> false


select
CASE
when ImageFile is NULL then 0
else 1
end as HasImage
from News