我们正在尝试将计算列添加到表中,此操作需要 20 多个小时才能完成:
json_unquote(coalesce(json_extract(`int_data`,'$.Item.IRefNum'),
json_extract(`m_dt`,'$.refId')))
数据库:MySQL 版本 5.7
表行:32,636,254
该表已存在,其中包含数据,无法删除。
我们正在尝试将计算列添加到表中,此操作需要 20 多个小时才能完成:
json_unquote(coalesce(json_extract(`int_data`,'$.Item.IRefNum'),
json_extract(`m_dt`,'$.refId')))
数据库:MySQL 版本 5.7
表行:32,636,254
该表已存在,其中包含数据,无法删除。
在将 SQL Server 的排序规则从 QL_Latin1_General_CP1_CI_AS 更改为 Latin1_General_CI_AI 后,我试图将服务器添加回 MSX 作为 CMS 中的目标,但登记失败。在 SQL 排序规则更改之前我忘记做的一件事是背叛服务器。现在我不确定是不是因为没有破坏服务器或更改排序规则导致了这种情况?无论如何,现在我需要在 MSX 中登记服务器并推送 SQL 代理监视作业。在更改之前,这台服务器已入伍。
我可以使用 SSMS 从 CMS 连接到目标服务器,反之亦然
目标 SQL Server 版本:2017 CMS SQL Server 版本:2017
尝试从 CMS 登记时出错:
===================================
MSX enlist failed for JobServer 'myserver.domain.sa'. (Microsoft.SqlServer.Smo)
------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=14.0.17285.0+((SSMS_Rel_17_4).180821-0238)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=MSX+enlist+JobServer&LinkId=20476
------------------------------
Program Location:
at Microsoft.SqlServer.Management.Smo.Agent.JobServer.MsxEnlist(String masterServer, String location)
at Microsoft.SqlServer.Management.SqlManagerUI.EnlistTsxActions.DoAction(ProgressItemCollection actions, Int32 index)
at Microsoft.SqlServer.Management.SqlStudio.Controls.ProgressItemCollection.DoWorkOnThread()
===================================
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
------------------------------
Program Location:
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType, Boolean retry)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries, Boolean retry)
at Microsoft.SqlServer.Management.Smo.Agent.JobServer.MsxEnlist(String masterServer, String location)
===================================
The enlist operation failed (reason: The time-out was exceeded while the server waited for a response from SQL Server Agent. Make sure that the SQL Server Agent service is running) (.Net SqlClient Data Provider)
------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=14.00.3281&EvtSrc=MSSQLServer&EvtID=22026&LinkId=20476
------------------------------
Server Name: myserver.domain.sa
Error Number: 22026
Severity: 16
State: 1
------------------------------
Program Location:
at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry)
尝试从目标服务器将此添加为目标时出错:
===================================
MSX enlist failed for JobServer 'myserver.domain.sa'. (Microsoft.SqlServer.Smo)
------------------------------
For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=15.0.18206.0+((SSMS_Rel).191029-2112)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=MSX+enlist+JobServer&LinkId=20476
------------------------------
Program Location:
at Microsoft.SqlServer.Management.Smo.Agent.JobServer.MsxEnlist(String masterServer, String location)
at Microsoft.SqlServer.Management.SqlManagerUI.EnlistTsxActions.DoAction(ProgressItemCollection actions, Int32 index)
at Microsoft.SqlServer.Management.SqlStudio.Controls.ProgressItemCollection.DoWorkOnThread()
===================================
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
------------------------------
Program Location:
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType, Boolean retry)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries, Boolean retry)
at Microsoft.SqlServer.Management.Smo.Agent.JobServer.MsxEnlist(String masterServer, String location)
===================================
The enlist operation failed (reason: The time-out was exceeded while the server waited for a response from SQL Server Agent. Make sure that the SQL Server Agent service is running) (.Net SqlClient Data Provider)
------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=14.00.3281&EvtSrc=MSSQLServer&EvtID=22026&LinkId=20476
我似乎无法在网上找到很多关于这个问题的信息。任何帮助,将不胜感激。
我是 powershell 和 dbatools 的新手。这是一个很棒的工具,但是当我尝试将几个不同命令的结果组合成一个要导出到 csv 的数组时,它并没有提供预期的结果。它只打印第一个命令的输出。有人可以帮我加入所有三个变量的结果吗?我要做的是编写一个脚本来自动生成我们的 SQL Server 库存和容量评估报告。
到目前为止,这是我的代码:
$Servers = Get-Content 'C:\Users\temp\ServerList.txt'
$reportData = @()
foreach ($Server in $Servers) {
if (Test-Connection -ComputerName $Server -Count 1 -ErrorAction 'SilentlyContinue') {
$OSInfo=Get-DbaOperatingSystem -ComputerName MyServer| Select ComputerName, OSVersion, Version, Architecture
$DiskSPace=Get-DbaDiskSpace -ComputerName MyServer| Select ComputerName, Label, Name, Capacity, Free
$SQLInstanceInfo=Connect-DbaInstance -SqlInstance MyServer | Select DbaInstanceName, Edition, NetPort, IsClustered, Processors, ProductLevel, ServiceName,
$reportData +=$OSInfo
$reportData +=$DiskSPace
$reportData +=$SQLInstanceInfo
}
else {
write-host $Server "not connected"
}
}
Write-Output $reportData | Format-Table
我从来没有在 SQL Server 2000 上工作过,我需要检查将以下信息捕获到一个特定的数据库。
DBName CurrentDate SPID BatchDuration ApplicationName HostName LoginName SQLQuery
我尝试了下面的查询,但它首先没有返回任何信息或不满足要求,因为它没有捕获 ApplicationName 和 SQL 文本。
SELECT
DB_NAME(dbid) as dbName
, CURRENT_TIMESTAMP as CurrentDate
, P.spid
, right(convert(varchar,
dateadd(ms, datediff(ms, P.last_batch, getdate()), '2000-01-01'),
121), 12) as 'batch_duration'
, P.program_name
, P.hostname
, P.loginame
, P.cmd
from master.dbo.sysprocesses P
where P.spid > 50
and P.status not in ('background', 'sleeping')
and P.cmd not in ('AWAITING COMMAND'
,'MIRROR HANDLER'
,'LAZY WRITER'
,'CHECKPOINT SLEEP'
,'RA MANAGER')
and DB_NAME(dbid) ='master'
order by batch_duration desc
此外,当我尝试通过 SQL 代理设置此脚本时,它只允许每隔一分钟运行一次。有没有其他更好的方法可以像每 5 秒运行一次?或者也许我应该使用另一种方法,例如通过触发器捕获带有所需信息的登录?
任何输入将不胜感激。
我正在寻找一种最佳实践/选项,仅将一张表从 Prod 刷新到 Staging 环境。
技术细节:
我正在考虑的一些选项:
我的第一个偏好是选择选项 3。这是一个不错的选择吗?如果是的话,有人可以指导我编写脚本。
我需要为我们整个生产环境的索引提出报告:
我在网上找到了以下脚本并根据我的要求对其进行了修改,并尝试使用游标(我也尝试过 sp_MSForEachDb)执行它以从实例中的所有数据库中获取结果。该脚本应显示特定数据库中的所有完全重复索引。
尽管我将查询用双引号括起来,但我总是收到太多错误。
如果我在没有循环的情况下运行脚本,它会正确返回结果。
请查看脚本和我在脚本下方遇到的错误。在过去的几天里,我一直在为此苦苦挣扎,并且在这里 和许多其他帖子上详尽地查看了在线内容,但无法解决这个问题。
我需要将电子邮件正文中的结果发送到我们所有服务器的分发列表。所以,如果有人对如何建立它有更好的想法,我将不胜感激。
脚本:
DECLARE @db_name AS nvarchar(max)
DECLARE c_db_names CURSOR FOR
SELECT name
FROM sys.databases
WHERE name NOT IN('master', 'model', 'msdb', 'tempdb') and state <> 1
OPEN c_db_names
FETCH c_db_names INTO @db_name
WHILE @@Fetch_Status = 0
BEGIN
IF OBJECT_ID('tempdb..#IndexTemp') IS NOT NULL DROP Table #IndexTemp --If exist drop the temp table.
EXEC('
Begin
USE ' + '[' + @db_name + ']' + '
;WITH CTE_INDEX_DATA AS (
SELECT
SCHEMA_DATA.name AS schema_name,
TABLE_DATA.name AS table_name,
INDEX_DATA.name AS index_name,
STUFF((SELECT '', '' + COLUMN_DATA_KEY_COLS.name + '' '' + CASE WHEN INDEX_COLUMN_DATA_KEY_COLS.is_descending_key = 1 THEN ''DESC'' ELSE ''ASC'' END -- Include column order (ASC / DESC)
FROM sys.tables AS T
INNER JOIN sys.indexes INDEX_DATA_KEY_COLS
ON T.object_id = INDEX_DATA_KEY_COLS.object_id
INNER JOIN sys.index_columns INDEX_COLUMN_DATA_KEY_COLS
ON INDEX_DATA_KEY_COLS.object_id = INDEX_COLUMN_DATA_KEY_COLS.object_id
AND INDEX_DATA_KEY_COLS.index_id = INDEX_COLUMN_DATA_KEY_COLS.index_id
INNER JOIN sys.columns COLUMN_DATA_KEY_COLS
ON T.object_id = COLUMN_DATA_KEY_COLS.object_id
AND INDEX_COLUMN_DATA_KEY_COLS.column_id = COLUMN_DATA_KEY_COLS.column_id
WHERE INDEX_DATA.object_id = INDEX_DATA_KEY_COLS.object_id
AND INDEX_DATA.index_id = INDEX_DATA_KEY_COLS.index_id
AND INDEX_COLUMN_DATA_KEY_COLS.is_included_column = 0
ORDER BY INDEX_COLUMN_DATA_KEY_COLS.key_ordinal
FOR XML PATH('')), 1, 2, '') AS key_column_list ,
STUFF(( SELECT '', '' + COLUMN_DATA_INC_COLS.name
FROM sys.tables AS T
INNER JOIN sys.indexes INDEX_DATA_INC_COLS
ON T.object_id = INDEX_DATA_INC_COLS.object_id
INNER JOIN sys.index_columns INDEX_COLUMN_DATA_INC_COLS
ON INDEX_DATA_INC_COLS.object_id = INDEX_COLUMN_DATA_INC_COLS.object_id
AND INDEX_DATA_INC_COLS.index_id = INDEX_COLUMN_DATA_INC_COLS.index_id
INNER JOIN sys.columns COLUMN_DATA_INC_COLS
ON T.object_id = COLUMN_DATA_INC_COLS.object_id
AND INDEX_COLUMN_DATA_INC_COLS.column_id = COLUMN_DATA_INC_COLS.column_id
WHERE INDEX_DATA.object_id = INDEX_DATA_INC_COLS.object_id
AND INDEX_DATA.index_id = INDEX_DATA_INC_COLS.index_id
AND INDEX_COLUMN_DATA_INC_COLS.is_included_column = 1
ORDER BY INDEX_COLUMN_DATA_INC_COLS.key_ordinal
FOR XML PATH('')), 1, 2, '') AS include_column_list,
INDEX_DATA.is_disabled -- Check if index is disabled before determining which dupe to drop (if applicable)
FROM sys.indexes INDEX_DATA
INNER JOIN sys.tables TABLE_DATA
ON TABLE_DATA.object_id = INDEX_DATA.object_id
INNER JOIN sys.schemas SCHEMA_DATA
ON SCHEMA_DATA.schema_id = TABLE_DATA.schema_id
WHERE TABLE_DATA.is_ms_shipped = 0
AND INDEX_DATA.type_desc IN (''NONCLUSTERED'', ''CLUSTERED'')
)
--Insert all records into a temp table #IndexTemp with appropriate filters:
SELECT * INTO #IndexTemp
FROM CTE_INDEX_DATA DUPE1
WHERE EXISTS
(SELECT * FROM CTE_INDEX_DATA DUPE2
WHERE DUPE1.schema_name = DUPE2.schema_name
AND DUPE1.table_name = DUPE2.table_name
AND DUPE1.key_column_list = DUPE2.key_column_list
AND ISNULL(DUPE1.include_column_list, '') = ISNULL(DUPE2.include_column_list, '')
AND DUPE1.index_name <> DUPE2.index_name)
AND INDEX_NAME NOT LIKE (''%PK%'')
--Return duplicate tbale_names only
SELECT * from #IndexTemp WHERE table_name IN
(SELECT table_name FROM #IndexTemp GROUP BY table_name HAVING COUNT(*) > 1)
ORDER BY table_name
END')
FETCH c_db_names INTO @db_name
END
CLOSE c_db_names
DEALLOCATE c_db_names
对于每个数据库,我都会收到以下错误:
消息 156,级别 15,状态 1,第 24 行
关键字“AS”附近的语法不正确。
消息 156,级别 15,状态 1,第 38 行
关键字“ORDER”附近的语法不正确。
消息 4145,级别 15,状态 1,第 59 行在
“AND”附近预期条件的上下文中指定的非布尔类型的表达式。
我正在尝试使用以下代码找出所有数据库中是否存在特定的索引名称“Test”。对于实例中的所有数据库,我得到的结果始终是“不存在”。但是,索引确实存在于少数数据库中。有人可以指出代码错误的地方吗?
DECLARE @Name AS VARCHAR(100)
DECLARE dbCur CURSOR FOR
SELECT name FROM SYS.databases where name not in ('master','msdb','model','tempdb')
OPEN dbCur
FETCH NEXT FROM dbCur
INTO @Name
WHILE @@FETCH_STATUS = 0
BEGIN
--Check if index exist
EXECUTE('USE [' + @Name + ']')
If 1 = ((SELECT COUNT(*) as index_count
FROM sys.indexes
WHERE object_id = OBJECT_ID('dbo.TestTable')
AND name = 'Test'))
Print 'INDEX EXIST ' + @Name;
ELSE
Print 'INDEX DOES NOT EXIST ' + @Name;
FETCH NEXT FROM dbCur
INTO @Name
END
CLOSE dbCur
DEALLOCATE dbCur
结果:
INDEX DOES NOT EXIST DB1
INDEX DOES NOT EXIST DB2
INDEX DOES NOT EXIST DB3
INDEX DOES NOT EXIST DB4