SQL

SQL SSRS: creating multiple parameters and allowing search on single or multiple entries

Posted on Updated on

I built an SSRS report for one of my colleagues and it took me a little bit to figure how to properly setup the Parameters.  I am hoping this would help somebody out there that may run into same issue.

Here is the scenario:

I created 4 parameters named ‘Asset Barcode, Asset Serial, Purchase Order and Former Tag’.  And if all these 4 parameters are set to blank, then all data should be displayed.  And if any of the parameters are blank, then it will not be included in the query.

For the past couple of days, I have been testing and searching online on how to get this working, but it seems that a lot of people are suggesting to create a stored procedure which is very cumbersome.  Without the correct script, the blank parameters are still being included in the query and thereby not giving an accurate result.

Here is how my script looks like that worked:

NOTE: DO NOT copy and paste the code below, the commas and quotes are not formatted correctly in WordPress.

SELECT
Assets.CreatedBy
,Assets.ModifiedBy
,Assets.[General/Asset Name]
,Assets.[General/Site_id]
,Assets.[General/Loc_id]
,Assets.[Assets/Classification]
,Assets.[Assets/Usage]
,Assets.[Assets/Condition]
,Assets.[Assets/Inventory Date]
,Assets.[Assets/Data Source]
,Assets.[Assets/Serial Number]
,Assets.[Assets/Former Tag No]
,Assets.[Assets/Equipment/Model]
,Assets.DateCreated
,Assets.DateModified
,Assets.[General/Purchase Order]
,Assets.[General/Barcode]
,Assets.[Employee Details/Employee Notes]
,Assets.[Assets/Mfg]
,Assets.[Assets/Category]
FROM
Assets

WHERE

(@Asset_Barcode = or Assets.[General/Barcode] LIKE ‘%’ + @Asset_Barcode + ‘%’) and
(@Asset_Serial = or Assets.[Assets/Serial Number] LIKE ‘%’ + @Asset_Serial + ‘%’) and
(@Purchase_Order = or Assets.[General/Purchase Order] LIKE ‘%’ + @Purchase_Order + ‘%’) and
(@Former_Tag = or Assets.[Assets/Former Tag No] LIKE ‘%’ + @Former_Tag + ‘%’)

Advertisements

Workaround for syspolicy_purge_history step 3 error on SQL 2012

Posted on Updated on

I have started setting up a couple of servers with SQL 2012 SP2.  And I noticed that the syspolicy_purge_history job is failing on Step 3 with the following error message:

A job step received an error at line 1 in a Powershell script.  The corresponding line is ‘set-executionpolicy RemoteSigned -scope process -Force’.  Correct the script and reschedule the job.

SQL2012_syspolicy

So far I have not seen any valid resolution online, so as a workaround, I have setup the Execution policy to UNDEFINED on the following registry entry:

Computer\HKLM\Software\PowerShell\1\Shelllds\Microsoft.SqlServer.Management.PowerShell.sqlps110
Execution Policy, REG_SZ,Undefined

And as a warning, changing the execution policy to UNDEFINED may open security holes on your server.  If you found a fix for this issues, please feel free to leave a comment so I can update this post.

Update:  I found a Microsoft KB article that talks about adding a GPO to correct the issue.  http://support.microsoft.com/kb/2995870

Thank you

 

How to create a list of servers to be managed in SSMS

Posted on

Most of the time when you try to connect to SQL servers, you often have to connect and enter in the name of the server/instance or you have to click on the drop down to show the previous server you have connected to.  That works for me most of the time, but I am going to show you how to add those servers in such a way that you can easily connect to them by just clicking on the server name.  And you can even setup different types of authentication used and connection properties.

First, you have to open SSMS and select View, then Registered Servers.  And then you can add connections to the servers and instances via the roles assigned on your SQL server.

Registered_Servers

Select the ‘Database Engine’ icon if you would like to connect to the main database, right-click and select ‘New Server Registration’.   And setup the server connection properties.

Server_Registration_Properties

Finding out most recent queries run on a specific database

Posted on

I found this useful script online to find out the queries run on a specific database.  This script is useful when you are trying to go back and find out what was ran on the database.

 

SELECT deqs.last_execution_time AS [Time], dest.text AS [Query], dest.*

FROM sys.dm_exec_query_stats AS deqs

CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

WHERE dest.dbid = DB_ID(‘dbname’)

ORDER BY deqs.last_execution_time DESC

 

Reference:  http://blog.sqlauthority.com/2008/01/03/sql-server-2005-last-ran-query-recently-ran-query/

How to move a user database to a different location

Posted on Updated on

Here are a couple of ways to move the database files to a different location other than the default.

Using SSMS:

1.  Using the SQL Server Management Console, right-click on the database, go to Task and Detach.  You may also have to restrict access to the database to SINGLE_USER before you can perform the Detach if there is a process currently using it.  To set it to SINGLE_USER mode, right-click on the database, properties, Options and scroll down to Restrict Access.

2.  Once the database is detached, copy/move the data file (.MDF) and log file (.LDF) to the new location, and then Attach the database back.  Right-click on Databases, select Attach.  Hit Add on the Attach Databases window and then browse to the new data file (.MDF) location and select the database.  If the log file (.LDF) is located on a separate disk, then you will also have to specify the path for the log file under the Database details window.  Then hit OK.

Using T-SQL scripts with Detach:

1.  You can run the following scripts to Detach, copy/move the data files and then perform the Attach command.  I will use the TEST database with an example path.

USE MASTER;

GO

ALTER DATABASE TEST

SET SINGLE_USER

WITH ROLLBACK IMMEDIATE;

GO

EXEC MASTER.DBO.SP_DETACH_DB @dbname = N’TEST

CREATE DATABASE [TEST] ON

( FILENAME = N’Y:\DATABASE\TEST.mdf‘ ),

( FILENAME = N’Z:\LOG\TEST_log.ldf‘ )

FOR ATTACH

GO

Using T-SQL Scripts with Alter Command:

ALTER DATABASE [TEST] SET OFFLINE

WITH ROLLBACK IMMEDIATE

GO

— Please note that the EXEC XP_CMDSHELL command might not work if it is not enabled in your SQL server.  You will need to enable it first or you can substitute this command with manually copying/moving the data files.

EXEC XP_CMDSHELL ‘COPY “C:\OLD_LOCATION\TEST.mdf” “Y:\DATABASE\TEST.mdf“‘

GO

EXEC XP_CMDSHELL ‘COPY “C:\OLD_LOCATION\TEST_LOG.ldf” “Z:\LOG\TEST_log.ldf“‘

GO

ALTER DATABASE [TEST] MODIFY FILE (NAME = ‘TEST‘, FILENAME = ‘Y:\DATABASE\TEST.mdf“)

GO

ALTER DATABASE [TEST] MODIFY FILE (NAME = ‘TEST_log‘, FILENAME = ‘Z:\LOG\TEST_log.ldf‘)

GO

How to move System database locations

Posted on Updated on

–Move Model Database
USE master;
GO
ALTER DATABASE model
MODIFY FILE (NAME = modeldev,FILENAME = ‘E:\DATA\model.mdf’);
GO
ALTER DATABASE model
MODIFY FILE (NAME = Modellog,FILENAME = ‘E:\DATA\modelLog.ldf’);
GO

–Move MSDB Database
USE master;
GO
ALTER DATABASE msdb
MODIFY FILE (NAME = MSDBData,FILENAME = ‘E:\DATA\MSDBData.mdf’);
GO
ALTER DATABASE msdb
MODIFY FILE (NAME = MSDBLog, FILENAME = ‘E:\DATA\MSDBLog.ldf’);
GO

–Move Tempdb Database
USE master;
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = ‘E:\DATA\tempdb.mdf’);
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = ‘E:\DATA\tempLog.ldf’);
GO

To move the master database:

In SQL Server configuration manager, edit the advanced properties for the SQL Server Service.

Change the startup parameters to the new location of the files, –l flag refers to log destination and –d flag refers to data file destination:

How to sort data pulled from sp_who2

Posted on Updated on

— using a temporary table

CREATE TABLE #sp_who2 (SPID INT,Status VARCHAR(255),
Login  VARCHAR(255),HostName  VARCHAR(255),
BlkBy  VARCHAR(255),DBName  VARCHAR(255),
Command VARCHAR(255),CPUTime INT,
DiskIO INT,LastBatch VARCHAR(255),
ProgramName VARCHAR(255),SPID2 INT,
REQUESTID INT)
INSERT INTO #sp_who2 EXEC sp_who2
SELECT      *
FROM        #sp_who2
— Add any filtering of the results here :
WHERE       DBName = ‘<database name’
— Add any sorting of the results here :
ORDER BY    DBName ASC

DROP TABLE #sp_who2

— using a table variable

declare @tempTable table (SPID INT,Status VARCHAR(255),
Login  VARCHAR(255),HostName  VARCHAR(255),
BlkBy  VARCHAR(255),DBName  VARCHAR(255),
Command VARCHAR(255),CPUTime INT,
DiskIO INT,LastBatch VARCHAR(255),
ProgramName VARCHAR(255),SPID2 INT,
REQUESTID INT);

INSERT INTO @tempTable
EXEC sp_who2

select *
from @tempTable
where DBName = ‘<database name’