SQL
Práce se soubory
UPDATE z disku
UPDATE [InvoiceIn] SET [InvoiceFile] = (SELECT * FROM OPENROWSET (BULK 'I:\Desktop\Invoice.pdf', SINGLE_BLOB) AS FileData) WHERE [InvoiceID] = 'II11X0001MFW'
Metadata
Hierarchie cizích klíčů
DECLARE @tableName sysname = NULL;
DECLARE @printDepth int = 5;
DECLARE @i nvarchar(4) = N'--> ';
DECLARE @c1 sysname, @c2 sysname, @c3 sysname, @c4 sysname, @c5 sysname;
DECLARE @deleteAction nvarchar(20), @updateAction nvarchar(20), @constraintName sysname;
DECLARE @childColumn sysname, @parentColumn sysname;
SET NOCOUNT ON
DECLARE @Dependencies table(Child sysname, Parent sysname, Pass int, ChildColumn sysname, ParentColumn sysname,
DeleteAction nvarchar(60), UpdateAction nvarchar(60), ConstraintName sysname)
INSERT INTO @Dependencies
SELECT DISTINCT OBJECT_NAME(sys.foreign_keys.parent_object_id), OBJECT_NAME(sys.foreign_keys.referenced_object_id), 0,
COL_NAME(sys.foreign_key_columns.parent_object_id, parent_column_id),
COL_NAME(sys.foreign_key_columns.referenced_object_id, referenced_column_id),
delete_referential_action_desc, update_referential_action_desc, name
FROM sys.foreign_keys INNER JOIN sys.foreign_key_columns ON sys.foreign_keys.object_id = sys.foreign_key_columns.constraint_object_id
WHERE sys.foreign_keys.parent_object_id <> sys.foreign_keys.referenced_object_id ORDER BY 1, 2;
UPDATE @Dependencies SET Pass = 1 WHERE Child NOT IN (SELECT Parent FROM @Dependencies);
DECLARE @loop int = 2;
WHILE @loop < @printDepth
BEGIN
UPDATE @Dependencies SET Pass = @loop
WHERE Pass = 0 AND Parent IN (SELECT Parent FROM @Dependencies AS d1 WHERE Pass = 0 AND NOT EXISTS
(SELECT * FROM @Dependencies AS d2 WHERE d1.Child = d2.Parent AND d2.Pass = 0));
SET @loop = @loop + 1;
END
PRINT N'=== ON DELETE / ON UPDATE children of ' + @tableName + N' ===';
PRINT N'';
SELECT Parent, ParentColumn, Child, ChildColumn, Pass, DeleteAction, UpdateAction, ConstraintName
FROM @Dependencies WHERE Parent = @tableName OR @tableName IS NULL
ORDER BY Parent, Child, ParentColumn, ChildColumn
SELECT @c1 = MIN(Child) FROM @Dependencies WHERE Parent = @tableName
WHILE @c1 IS NOT NULL BEGIN
SELECT @deleteAction = DeleteAction, @updateAction = UpdateAction FROM @Dependencies WHERE Child = @c1 AND Parent = @tableName;
IF @printDepth >=1 PRINT @i + CAST(@deleteAction AS nchar(10)) + N'/ ' + CAST(@updateAction AS nchar(10)) + CAST(@c1 AS nchar(32));
SELECT @c2 = MIN(Child) FROM @Dependencies WHERE Parent = @c1;
WHILE @c2 IS NOT NULL BEGIN
SELECT @deleteAction = DeleteAction, @updateAction = UpdateAction FROM @Dependencies WHERE Child = @c2 AND Parent = @c1;
IF @printDepth >=2 PRINT @i+@i + CAST(@deleteAction AS nchar(10)) + N'/ ' + CAST(@updateAction AS nchar(10)) + CAST(@c1 AS nchar(32));
SELECT @c3 = MIN(Child) FROM @Dependencies WHERE Parent = @c2;
WHILE @c3 IS NOT NULL BEGIN
SELECT @deleteAction = DeleteAction, @updateAction = UpdateAction FROM @Dependencies WHERE Child = @c2 AND Parent = @c1;
IF @printDepth >=2 PRINT @i+@i + CAST(@deleteAction AS nchar(10)) + N'/ ' + CAST(@updateAction AS nchar(10)) + CAST(@c1 AS nchar(32));
SELECT @c4 = MIN(Child) FROM @Dependencies WHERE Parent = @c3;
WHILE @c5 IS NOT NULL BEGIN
SELECT @deleteAction = DeleteAction, @updateAction = UpdateAction FROM @Dependencies WHERE Child = @c2 AND Parent = @c1;
IF @printDepth >=2 PRINT @i+@i + CAST(@deleteAction AS nchar(10)) + N'/ ' + CAST(@updateAction AS nchar(10)) + CAST(@c1 AS nchar(32));
SELECT @c5 = MIN(Child) FROM @Dependencies WHERE Parent = @c4;
WHILE @c5 IS NOT NULL BEGIN
SELECT @deleteAction = DeleteAction, @updateAction = UpdateAction FROM @Dependencies WHERE Child = @c2 AND Parent = @c1;
IF @printDepth >=2 PRINT @i+@i + CAST(@deleteAction AS nchar(10)) + N'/ ' + CAST(@updateAction AS nchar(10)) + CAST(@c1 AS nchar(32));
END;
SELECT @c4 = MIN(Child) FROM @Dependencies WHERE Parent = @c3 AND Child > @c4;
END;
SELECT @c3 = MIN(Child) from @Dependencies where Parent = @c2 AND Child > @c3;
END;
SELECT @c2 = MIN(Child) from @Dependencies where Parent = @c1 AND Child > @c2;
END;
SELECT @c1 = MIN(Child) from @Dependencies where Parent = @tableName AND Child > @c1;
END;
Procedury
SET vs. SELECT
- SET is the ANSI standard for variable assignment, SELECT is not.
- SET can only assign one variable at a time, SELECT can make multiple assignments at once.
- SCALAR If assigning from a query, SET can only assign a scalar value. If the query returns multiple values/rows then SET will raise an error. SELECT will assign one of the values to the variable and hide the fact that multiple values were returned (so you'd likely never know why something was going wrong elsewhere - have fun troubleshooting that one)
- NULL When assigning from a query if there is no value returned then SET will assign NULL, where SELECT will not make the assignment at all (so the variable will not be changed from it's previous value)
- As far as speed differences - there are no direct differences between SET and SELECT. However SELECT's ability to make multiple assignments in one shot does give it a slight speed advantage over SET.
http://vyaskn.tripod.com/differences_between_set_and_select.htm
programovani/sql.txt · Poslední úprava: 20.11.2011 15:36 autor: miloush