Differenze tra le versioni di "MSSQL"
Da WikiSitech.
Vai alla navigazioneVai alla ricerca(18 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
− | == Microsoft SQL Server 2000 - Appunti di gestione | + | = Microsoft SQL Server 2008 - Appunti di gestione = |
− | + | == Spostare database utente su dischi diversi == | |
+ | Il modo più semplice in assoluto è quello di procedere secondo questa sequenza: | ||
+ | # '''detach''' del database | ||
+ | # spostamento dei file nella nuova posizione | ||
+ | # '''attach''' dei file dalla nuova posizione | ||
+ | <br> | ||
+ | == Spostare alcuni dei database di sistema su dischi diversi == | ||
+ | Per spostare i database di sistema su un disco diverso, procedere come segue: | ||
+ | * Verificare la dislocazione attuale dei database | ||
+ | <code sql> | ||
+ | SELECT name, physical_name AS CurrentLocation, state_desc | ||
+ | FROM sys.master_files; | ||
+ | </code> | ||
+ | * Modificare la definizione dello storage al nuovo percorso | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | -- | ||
+ | -- In questo esempio ricollochiamo il DB di sistema 'distribuzione' | ||
+ | ALTER DATABASE distribuzione MODIFY FILE ( NAME = 'distribuzione', FILENAME = 'E:\MSSQLData\distribuzione.MDF' ) | ||
+ | ALTER DATABASE distribuzione MODIFY FILE ( NAME = 'distribuzione_log', FILENAME = 'E:\MSSQLData\distribuzione.LDF' ) | ||
+ | </syntaxhighlight> | ||
+ | * Interrompere l'istanza di SQL Server | ||
+ | * Spostare i file nella nuova posizione | ||
+ | * Riavviare l'istanza di SQL Server | ||
+ | <br> | ||
+ | Questi appunti sono stati tratti da un articolo di [http://msdn.microsoft.com/it-it/library/ms345408(v=sql.100).aspx MSDN].<br> | ||
+ | |||
+ | = Microsoft SQL Server 2000 - Appunti di gestione = | ||
+ | == Trasferimento login e password fra database diversi == | ||
Vedi articolo [http://support.microsoft.com/kb/246133/en-us 246133] sul sito del supporto Microsoft | Vedi articolo [http://support.microsoft.com/kb/246133/en-us 246133] sul sito del supporto Microsoft | ||
− | === Estrazione record in attesa di replica transazionale | + | == Risoluzione di utenze orfane di login (post restore da un database diverso) == |
− | < | + | === SQL Server 2005/2008 === |
+ | Vedi man page [http://msdn.microsoft.com/en-us/library/ms176060.aspx ALTER USER] sul sito del supporto Microsoft | ||
+ | Esempio: | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | ALTER USER pinco WITH NAME=pinco, LOGIN=pinco; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Estrazione record in attesa di replica transazionale == | ||
+ | <syntaxhighlight lang="sql"> | ||
SELECT name as 'Agent', SUM(UndelivCmdsInDistDB) as 'Cmds' | SELECT name as 'Agent', SUM(UndelivCmdsInDistDB) as 'Cmds' | ||
FROM distribution.dbo.MSdistribution_agents a | FROM distribution.dbo.MSdistribution_agents a | ||
INNER JOIN distribution.dbo.MSdistribution_status st on st.agent_id = a.id | INNER JOIN distribution.dbo.MSdistribution_status st on st.agent_id = a.id | ||
GROUP BY a.name | GROUP BY a.name | ||
− | </ | + | </syntaxhighlight> |
− | + | = Microsoft SQL Server 2005 - Installazione in modalità unattended = | |
− | + | == Installazione di una nuova istanza == | |
Start /wait <CD or DVD Drive>\DISK1\setup.exe /qb INSTANCENAME=<InstanceName> ADDLOCAL=All PIDKEY=<pidkey value with no "-"> SAPWD=<StrongPassword> SQLACCOUNT=<domain\user> SQLPASSWORD=<DomainUserPassword> AGTACCOUNT=<domain\user> AGTPASSWORD=<DomainUserPassword> SQLBROWSERACCOUNT=<domain\user> SQLBROWSERPASSWORD=<DomainUserPassword> | Start /wait <CD or DVD Drive>\DISK1\setup.exe /qb INSTANCENAME=<InstanceName> ADDLOCAL=All PIDKEY=<pidkey value with no "-"> SAPWD=<StrongPassword> SQLACCOUNT=<domain\user> SQLPASSWORD=<DomainUserPassword> AGTACCOUNT=<domain\user> AGTPASSWORD=<DomainUserPassword> SQLBROWSERACCOUNT=<domain\user> SQLBROWSERPASSWORD=<DomainUserPassword> | ||
<br> | <br> | ||
Per ulteriori dettagli vedi l'[http://msdn2.microsoft.com/en-us/library/ms144259.aspx articolo] estratto da book online<br> | Per ulteriori dettagli vedi l'[http://msdn2.microsoft.com/en-us/library/ms144259.aspx articolo] estratto da book online<br> | ||
− | + | = Microsoft SQL Server 2005 - Appunti di gestione = | |
− | + | = Knowledge Base interna = | |
Vai alla pagina dedicata alla [[MSSQL-KB | KB]]. | Vai alla pagina dedicata alla [[MSSQL-KB | KB]]. | ||
<br> | <br> | ||
+ | = HOW TO = | ||
+ | == Elenca tutti i database con un determinato Recovery Model == | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | SELECT name, recovery_model_desc | ||
+ | FROM sys.databases | ||
+ | WHERE recovery_model_desc <> 'simple'; | ||
+ | </syntaxhighlight> | ||
+ | == Script per backup di alcuni filegroup == | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | BACKUP DATABASE PAPER FILEGROUP='<filegroup-name>' | ||
+ | , FILEGROUP='<filegroup-name' | ||
+ | TO DISK='disk-device' | ||
+ | </syntaxhighlight> | ||
+ | === Backup PRIMARY filegroup === | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | BACKUP DATABASE PAPER FILEGROUP='PRIMARY' | ||
+ | TO DISK='disk-device' | ||
+ | </syntaxhighlight> | ||
+ | == Determina lo spazio occupato da tutti i database di una istanza == | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | with fs | ||
+ | as | ||
+ | ( | ||
+ | select database_id, type, size * 8.0 / 1024 size | ||
+ | from sys.master_files | ||
+ | ) | ||
+ | select | ||
+ | name, | ||
+ | (select sum(size) from fs where type = 0 and fs.database_id = db.database_id) DataFileSizeMB, | ||
+ | (select sum(size) from fs where type = 1 and fs.database_id = db.database_id) LogFileSizeMB | ||
+ | from sys.databases db | ||
+ | </syntaxhighlight> | ||
+ | == Elenca tutte le tabelle nei vari FILEGROUP == | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | -- List all Objects and Indexes | ||
+ | -- per Filegroup / Partition and Allocation Type | ||
+ | -- including the allocated data size | ||
+ | SELECT DS.name AS DataSpaceName | ||
+ | ,AU.type_desc AS AllocationDesc | ||
+ | ,AU.total_pages / 128 AS TotalSizeMB | ||
+ | ,AU.used_pages / 128 AS UsedSizeMB | ||
+ | ,AU.data_pages / 128 AS DataSizeMB | ||
+ | ,SCH.name AS SchemaName | ||
+ | ,OBJ.type_desc AS ObjectType | ||
+ | ,OBJ.name AS ObjectName | ||
+ | ,IDX.type_desc AS IndexType | ||
+ | ,IDX.name AS IndexName | ||
+ | FROM sys.data_spaces AS DS | ||
+ | INNER JOIN sys.allocation_units AS AU | ||
+ | ON DS.data_space_id = AU.data_space_id | ||
+ | INNER JOIN sys.partitions AS PA | ||
+ | ON (AU.type IN (1, 3) | ||
+ | AND AU.container_id = PA.hobt_id) | ||
+ | OR | ||
+ | (AU.type = 2 | ||
+ | AND AU.container_id = PA.partition_id) | ||
+ | INNER JOIN sys.objects AS OBJ | ||
+ | ON PA.object_id = OBJ.object_id | ||
+ | INNER JOIN sys.schemas AS SCH | ||
+ | ON OBJ.schema_id = SCH.schema_id | ||
+ | LEFT JOIN sys.indexes AS IDX | ||
+ | ON PA.object_id = IDX.object_id | ||
+ | AND PA.index_id = IDX.index_id | ||
+ | ORDER BY DS.name | ||
+ | ,SCH.name | ||
+ | ,OBJ.name | ||
+ | </syntaxhighlight > | ||
+ | |||
+ | == Estrae elenco delle tabelle e relativo numero di righe in ognuna == | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | SELECT '[' + SCHEMA_NAME(t.schema_id) + '].[' + t.name + ']' AS fulltable_name, SCHEMA_NAME(t.schema_id) AS schema_name, t.name AS table_name, i.rows | ||
+ | FROM sys.tables AS t INNER JOIN | ||
+ | sys.sysindexes AS i ON t.object_id = i.id AND i.indid < 2 | ||
+ | WHERE SCHEMA_NAME(t.schema_id) NOT IN ('dbo') | ||
+ | ORDER BY 1 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Estrazione del contenuto di una tabella tramite BCP == | ||
+ | bcp [dbname].[schema].tabella out FileDiOutput.bcp -n -k -S SERVER\ISTANZA -T | ||
+ | |||
+ | == Importazione in tabella tramite BCP == | ||
+ | bcp [dbname].[schema].tabella in FileDiOutput.bcp -n -k -S SERVER\ISTANZA -T |
Versione attuale delle 10:43, 9 mag 2018
Indice
- 1 Microsoft SQL Server 2008 - Appunti di gestione
- 2 Microsoft SQL Server 2000 - Appunti di gestione
- 3 Microsoft SQL Server 2005 - Installazione in modalità unattended
- 4 Microsoft SQL Server 2005 - Appunti di gestione
- 5 Knowledge Base interna
- 6 HOW TO
- 6.1 Elenca tutti i database con un determinato Recovery Model
- 6.2 Script per backup di alcuni filegroup
- 6.3 Determina lo spazio occupato da tutti i database di una istanza
- 6.4 Elenca tutte le tabelle nei vari FILEGROUP
- 6.5 Estrae elenco delle tabelle e relativo numero di righe in ognuna
- 6.6 Estrazione del contenuto di una tabella tramite BCP
- 6.7 Importazione in tabella tramite BCP
Microsoft SQL Server 2008 - Appunti di gestione
Spostare database utente su dischi diversi
Il modo più semplice in assoluto è quello di procedere secondo questa sequenza:
- detach del database
- spostamento dei file nella nuova posizione
- attach dei file dalla nuova posizione
Spostare alcuni dei database di sistema su dischi diversi
Per spostare i database di sistema su un disco diverso, procedere come segue:
- Verificare la dislocazione attuale dei database
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files;
- Modificare la definizione dello storage al nuovo percorso
--
-- In questo esempio ricollochiamo il DB di sistema 'distribuzione'
ALTER DATABASE distribuzione MODIFY FILE ( NAME = 'distribuzione', FILENAME = 'E:\MSSQLData\distribuzione.MDF' )
ALTER DATABASE distribuzione MODIFY FILE ( NAME = 'distribuzione_log', FILENAME = 'E:\MSSQLData\distribuzione.LDF' )
- Interrompere l'istanza di SQL Server
- Spostare i file nella nuova posizione
- Riavviare l'istanza di SQL Server
Questi appunti sono stati tratti da un articolo di MSDN.
Microsoft SQL Server 2000 - Appunti di gestione
Trasferimento login e password fra database diversi
Vedi articolo 246133 sul sito del supporto Microsoft
Risoluzione di utenze orfane di login (post restore da un database diverso)
SQL Server 2005/2008
Vedi man page ALTER USER sul sito del supporto Microsoft Esempio:
ALTER USER pinco WITH NAME=pinco, LOGIN=pinco;
Estrazione record in attesa di replica transazionale
SELECT name as 'Agent', SUM(UndelivCmdsInDistDB) as 'Cmds'
FROM distribution.dbo.MSdistribution_agents a
INNER JOIN distribution.dbo.MSdistribution_status st on st.agent_id = a.id
GROUP BY a.name
Microsoft SQL Server 2005 - Installazione in modalità unattended
Installazione di una nuova istanza
Start /wait <CD or DVD Drive>\DISK1\setup.exe /qb INSTANCENAME=<InstanceName> ADDLOCAL=All PIDKEY=<pidkey value with no "-"> SAPWD=<StrongPassword> SQLACCOUNT=<domain\user> SQLPASSWORD=<DomainUserPassword> AGTACCOUNT=<domain\user> AGTPASSWORD=<DomainUserPassword> SQLBROWSERACCOUNT=<domain\user> SQLBROWSERPASSWORD=<DomainUserPassword>
Per ulteriori dettagli vedi l'articolo estratto da book online
Microsoft SQL Server 2005 - Appunti di gestione
Knowledge Base interna
Vai alla pagina dedicata alla KB.
HOW TO
Elenca tutti i database con un determinato Recovery Model
SELECT name, recovery_model_desc
FROM sys.databases
WHERE recovery_model_desc <> 'simple';
Script per backup di alcuni filegroup
BACKUP DATABASE PAPER FILEGROUP='<filegroup-name>'
, FILEGROUP='<filegroup-name'
TO DISK='disk-device'
Backup PRIMARY filegroup
BACKUP DATABASE PAPER FILEGROUP='PRIMARY'
TO DISK='disk-device'
Determina lo spazio occupato da tutti i database di una istanza
with fs
as
(
select database_id, type, size * 8.0 / 1024 size
from sys.master_files
)
select
name,
(select sum(size) from fs where type = 0 and fs.database_id = db.database_id) DataFileSizeMB,
(select sum(size) from fs where type = 1 and fs.database_id = db.database_id) LogFileSizeMB
from sys.databases db
Elenca tutte le tabelle nei vari FILEGROUP
-- List all Objects and Indexes
-- per Filegroup / Partition and Allocation Type
-- including the allocated data size
SELECT DS.name AS DataSpaceName
,AU.type_desc AS AllocationDesc
,AU.total_pages / 128 AS TotalSizeMB
,AU.used_pages / 128 AS UsedSizeMB
,AU.data_pages / 128 AS DataSizeMB
,SCH.name AS SchemaName
,OBJ.type_desc AS ObjectType
,OBJ.name AS ObjectName
,IDX.type_desc AS IndexType
,IDX.name AS IndexName
FROM sys.data_spaces AS DS
INNER JOIN sys.allocation_units AS AU
ON DS.data_space_id = AU.data_space_id
INNER JOIN sys.partitions AS PA
ON (AU.type IN (1, 3)
AND AU.container_id = PA.hobt_id)
OR
(AU.type = 2
AND AU.container_id = PA.partition_id)
INNER JOIN sys.objects AS OBJ
ON PA.object_id = OBJ.object_id
INNER JOIN sys.schemas AS SCH
ON OBJ.schema_id = SCH.schema_id
LEFT JOIN sys.indexes AS IDX
ON PA.object_id = IDX.object_id
AND PA.index_id = IDX.index_id
ORDER BY DS.name
,SCH.name
,OBJ.name
Estrae elenco delle tabelle e relativo numero di righe in ognuna
SELECT '[' + SCHEMA_NAME(t.schema_id) + '].[' + t.name + ']' AS fulltable_name, SCHEMA_NAME(t.schema_id) AS schema_name, t.name AS table_name, i.rows
FROM sys.tables AS t INNER JOIN
sys.sysindexes AS i ON t.object_id = i.id AND i.indid < 2
WHERE SCHEMA_NAME(t.schema_id) NOT IN ('dbo')
ORDER BY 1
Estrazione del contenuto di una tabella tramite BCP
bcp [dbname].[schema].tabella out FileDiOutput.bcp -n -k -S SERVER\ISTANZA -T
Importazione in tabella tramite BCP
bcp [dbname].[schema].tabella in FileDiOutput.bcp -n -k -S SERVER\ISTANZA -T