19 March 2015

Differenza tra UNION e UNION ALL

UNION
- è come un SELECT DISTINCT, quindi elimina i doppioni
- fa un ORDER BY dei record

UNION ALL
- lascia i doppioni
- mantiene l'ordine originale dei record

Creo due tabelle con la stessa struttura e le riempio:
DECLARE @a as table (a int, b varchar(10))
INSERT  @a (a, b) SELECT 20, 'bla'
INSERT @a (a, b) SELECT 10, 'foo'

DECLARE @b as table (a int, b varchar(10))
INSERT @b (a, b) SELECT 20, 'bla'

SELECT * FROM @a
SELECT * FROM @b
Risultati:
a           b
----------- ----------
20          bla
10          foo

a           b
----------- ----------
20          bla

UNION:
SELECT * FROM @a
UNION
SELECT * FROM @b
Risultati:
a           b
----------- ----------
10          foo
20          bla

UNION ALL:
SELECT * FROM @a
UNION ALL
SELECT * FROM @b
Risultati:
a           b
----------- ----------
20          bla
10          foo
20          bla

Dimostrazione che la UNION ALL è una DISTINCT:
SELECT DISTINCT *
FROM (
   SELECT * FROM @a
   UNION ALL
   SELECT * FROM @b
  ) as x
Risultati:
a           b
----------- ----------
10          foo
20          bla


Trovato qui...

... e qui.

17 March 2015

Impossibile visualizzare o creare diagrammi in MSSQL

Dopo aver fatto una restore, può capitare di non riuscire a visualizzare i diagrammi del database. Microsoft SQL Server Management Studio ritorna questo errore:

Impossibile installare gli oggetti di supporto per i diagrammi di database perché al database non è associato un proprietario valido. Per continuare, utilizzare innanzitutto la pagina File della finestra di dialogo Proprietà database o l'istruzione ALTER AUTHORIZATION per impostare il proprietario del database su un account di accesso valido, quindi aggiungere gli oggetti di supporto per i diagrammi di database.

o, in inglese:

Database diagram support objects cannot be installed because this database does not have a valid owner. To continue, first use the Files page of the Database Properties dialog box or the ALTER AUTHORIZATION statement to set the database owner to a valid login, then add the database diagram support objects.

Questa è la soluzione:

USE master
GO
ALTER AUTHORIZATION ON DATABASE::Northwind TO sa
GO
USE Northwind
GO
EXECUTE AS USER = N'dbo' REVERT
GO


Trovato qui.

16 March 2015

SQL: convert int to date wiith AGENT_DATETIME

MSDB function to convert from int to datetime!
SELECT no_time = msdb.dbo.agent_datetime(20150119, 0)

Result:
no_time
-----------------------
2015-01-19 00:00:00.000


With time:
SELECT with_time = msdb.dbo.agent_datetime(20150119, 171911)

Result:
with_time
-----------------------
2015-01-19 17:19:11.000

"Apri con" un file Markdown con MdWiki

OpenWithMdWiki.bat

@echo off
cls
rem web browser full path
set browserPath="C:\Program Files (x86)\Mozilla Firefox\Firefox.exe"

rem MDWiki path
set mdwikiPath=file:///C:/mdwiki.html#!

rem get Path+Name+Extension of the first batch parameter
set mdFile=%~pnx1

rem needed to replace the "%" characters
setLocal EnableDelayedExpansion

rem replace spaces with %20
set mdFile=!mdFile: =%%20!

rem substring(1,999): removes first character (it's a "\")
set mdFile=%mdFile:~1,999%

rem echo %mdFile%
%browserPath% %mdwikiPath%%mdFile%

set browserPath=
set mdwikiPath=
set mdFile=
EndLocal

Poi impostare il "tasto destro / apri con" e scegliere OpenWithMdWiki.bat


Info trovate:

Qui

Poi qui

Infine qui