xp_cmdshell error: ‘C:\Program’ is not recognized as an internal or external command,

Apparently in my 64-bit environment I can’t execute the following command.

DECLARE @vcCmd VARCHAR(8000)
SELECT @vcCmd = '"C:\Program Files\Winzip\WZZIP.exe" -es -a "E:\DATA\test.zip" "E:\DATA\test.dat"'
EXEC   master..xp_cmdshell @vcCmd = @vcCmd
/*
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
*/

The solution is I can execute other DOS commands, so let’s concatenate them.

DECLARE @vcCmd VARCHAR(8000)
SELECT @vcCmd = 'C: && CD "C:\Program Files\Winzip\" && WZZIP.exe -es -a "E:\DATA\test.zip" "E:\DATA\test1.dat"'
EXEC   master..xp_cmdshell @vcCmd = @vcCmd

Check SQL jobs which have run during daylight saving’s change hours.

What has run, during the last month, on Sunday’s between 1:59 AM and 3?
Use the to check on daylight saving’s jobs which might be missed.


SELECT MAX(master_admin.dbo.fn_INTtoDateTime(run_date, run_time) ),
j.name
FROM msdb.dbo.sysjobhistory h WITH (NOLOCK)
JOIN msdb.dbo.sysjobs j WITH (NOLOCK)
ON j.job_id = h.job_id
WHERE datename(dw, convert(varchar(10),run_date, 112) ) = 'Sunday'
AND run_time BETWEEN 015900 AND 030000
AND step_id = 0
AND run_date >= CONVERT(VARCHAR(8), DATEADD(day, -31, GETDATE()), 112)
GROUP BY j.name ,DATENAME(weekday, CONVERT(VARCHAR(10), run_date, 112))
ORDER BY
name

This uses one of my functions. fn_INTtoDateTime

[master_admin].[dbo].[fn_INTtoDateTime]

I use this when querying msdb.dbo.sysjobhistory.


USE [master_admin]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[fn_INTtoDateTime] (
@vcDate VARCHAR(8),
@vcTime VARCHAR(6)
)
RETURNS DATETIME
AS
BEGIN
SET @vcDate = RIGHT(RTRIM('00000000' + CONVERT(CHAR(8), @vcDate) + ' '), 8)
SET @vcTime = RIGHT(RTRIM('00000000' + CONVERT(CHAR(6), @vcTime) + ' '), 6)

IF @vcDate = '00000000'
SET @vcDate = LEFT(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(23), CAST(0 AS SMALLDATETIME), 121),
'-', ''), ':', ''),
SPACE(1), ''), 8)

RETURN (
CAST(SUBSTRING(@vcDate,1,4) + '-' + SUBSTRING(@vcDate,5,2) + '-' + SUBSTRING(@vcDate,7,2) + ' ' +
SUBSTRING(@vcTime,1,2) + ':' + SUBSTRING(@vcTime,3,2) + ':' + SUBSTRING(@vcTime,5,2)
AS DATETIME)
)
END

GO

SQL Server – Performance – Log file

Logfile hardware recommendations. By Tripp.

4) Not only should you try to isolate the transaction log to its own physical disk but you should make sure that the logical/physical disk configuration is as efficient as possible. 
   Try to use an isolated RAID 1 mirroring set if you don't need significant capacity. If you need a greater capacity OR you want better performance, 
   consider a combination of RAID 0 and RAID 1 (either RAID 0 + 1 or RAID 1 + 0). 
While RAID 0 + 1 can often offer better performance, RAID 1 + 0 offers better reliability.

RAID specifics.

SQL – All databases users all their roles, except reader

For every database return non-db_datareaders, dbo, and ## users database permissions.

-- Declaration of table variable for the storage of results
DECLARE @result AS TABLE (
 database_name SYSNAME,
 role_name SYSNAME,
 principal_name SYSNAME
 );

-- Insert the resultset from each database
INSERT INTO @result
 EXEC sp_msforeachdb N'USE [?]
SELECT CASE WHEN DB_NAME() IN (''master'',''msdb'',''model'',''tempdb'') THEN ''*'' + DB_NAME() ELSE DB_NAME() END AS database_name,
 USER_NAME(role_principal_id) AS [Role],
 USER_NAME(member_principal_id) AS [User]
FROM sys.database_principals p
INNER JOIN sys.database_role_members m
ON (p.principal_id = m.member_principal_id)
INNER JOIN sys.database_principals r
ON (m.role_principal_id = r.principal_id)
WHERE r.name NOT IN (''db_datareader'')
 AND USER_NAME(member_principal_id) <> ''dbo''
 AND USER_NAME(member_principal_id) NOT LIKE ''##%''
ORDER BY [Role],
 [User]
;'
-- Display the result
SELECT *
FROM @result
ORDER BY database_name,
 principal_name;

SQL Msg 15138: The database principal owns a schema in the database, and cannot be dropped.

Here’s my script to fix this error. This occurs when I restore a database to another server and want to remove the source server users.


DECLARE @vcSchemaName VARCHAR(128);
SET @vcSchemaName = 'schema name goes here'
SELECT s.name,
 'ALTER AUTHORIZATION ON SCHEMA::[' + @vcSchemaName + '] TO dbo;'
FROM sys.schemas s
WHERE s.principal_id = USER_ID(@vcSchemaName

Operation is not valid due to the current state of the object

This error occurs for me when I loop through a list of $CurrentServers. The solution was to add Out-Host to the end of the output line.

Operation is not valid due to the current state of the object
At line: 0 char: 0


$dsOut = Invoke-sqlcmd2  -ConnectionTimeout 10 -ServerInstance $CurrentServer -Query $SQL -Verbose -QueryTimeout 10 -Database master_admin -As DataTable

# Use Out-Host to prevent format-table errors
$dsOut |Select-Object * |ft -AutoSize |Out-Host


TSQL – Create an ASCII chart

This code shows the Symbol and HEX as well as DEC. Still to go is OCT.


WITH Symbols
 AS (
 SELECT -1 code,
 CHAR(ASCII(' ')) Symbol
 UNION ALL
 SELECT code + 1,
 CHAR(code + 1)
 FROM Symbols
 WHERE code + 1 <= 255
 )
 SELECT Symbol,
 ASCII(Symbol) AS [DEC],
 master.dbo.fn_varbintohexstr(CAST(Symbol AS VARBINARY)) AS HEX
 --,(ASCII(Symbol) % 8) AS [OCT]
 FROM Symbols
 ORDER BY [DEC]
OPTION (MAXRECURSION 256)