News for the ‘Databases’ Category

Crystal Reports: Passing parameter values from main to subreport

Crystal Reports: Passing parameter values from main to subreport
Tested Technologies : Crystal Reports Basic for Visual Studio 2008

Steps:
1. Create Main Report [Eg:myMainReport.rpt]
2. Create SubReport [Eg:mySubReport.rpt]
3. Pass parameters [From database/through code] to Main report
4. Choose which parameter(s) [Eg.{Discount}] you want to pass to Sub Report
5. Open mySubReport; Create a parameter in the format [Name]:{Pm-?ParameteName} [Eg:{Pm-?Discount}] as normal parameter field; Place this parameter in the app section of the report to display.
6. Open myMainReport; Select ‘mySubReport.rpt’; Right-Click, Click ‘Re-Import sub report’
7. Again Right-Click, Click ‘Change sub-report links’
8. In the new wizard choose ‘Available Fields’; then select ‘Discount’ under ‘Report Fields’; Drag this to ‘Field(s) to link to’ section
9. Under ‘?Discount field link’ dropdown list select {?Pm-?Discount}; This will gred-out ‘Select data in subreport based on field’ tick box and also its dropdown list,Finally click OK.
10. Done & Run to Check

Posted: September 14th, 2009
Categories: Crystal Reports
Tags:
Comments: 1 Comment.

Removing a solution/project from SourceSafe

@ Visual Source Safe:
1. Delete the solution/project

@ Visual Studio
1 – Go to the folder containing the solution files and delete the following:
mssccprj.scc
MyProject.vssscc
vssver.scc

2 – Open MyProject.sln in your favorite text editor and remove the following section:
GlobalSection(SourceCodeControl) = preSolution

EndGlobalSection

3 – Go to the folder containing the project files and delete the following:
MyProject.vbproj.vspscc
mssccprj.scc
vssver.scc

4 – Open MyProject.vbproj in your text editor and remove the following lines:
SccProjectName = “SAK”
SccLocalPath = “SAK”
SccAuxPath = “SAK”
SccProvider = “SAK”

Posted: August 20th, 2009
Categories: Databases
Tags: ,
Comments: No Comments.

SQL server backup Error 3154

SQL Sever Error 3154: The backup set holds a backup of a database other than the existing database.

Tested only on MS SERVER 2008

USE master
GO
 
-- Database to restore : DJ_ADMIN
-- Resore With: DJ_ADMIN_Latest.bak
-- Backup existing database's to : DJ_ADMIN_Old.bak;DJ_ADMIN_Old_Log.bak
-- Database to restore : DJ_ADMIN
 
ALTER DATABASE DJ_ADMIN
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE DJ_ADMIN
FROM DISK = 'C:\Backups\DJ_ADMIN_Latest.bak'
WITH MOVE 'DJ_ADMIN' TO 'C:\Backups\DJ_ADMIN_Old.mdf',
MOVE 'DJ_ADMIN_Log' TO 'C:\Backups\DJ_ADMIN_Old_Log.ldf',
REPLACE
Posted: July 22nd, 2009
Categories: T-SQL
Tags: , , ,
Comments: No Comments.

Resetting/Changing the identity[auto-increment number] column value of a table

Resetting/Changing the identity[auto-increment number] column value of a table

DBCC CHECKIDENT checks the current identity value for the specified table and, if it is needed, changes the identity value. You can also use DBCC CHECKIDENT to manually set a new current identity value for the identity column

Tested only on MS SERVER 2008

Table to alter:

CREATE TABLE Admin.DocumentType_List
(
	DocumentType_ID INT IDENTITY(0,1) NOT NULL,
	DocumentType NVARCHAR(250) NOT NULL,
	Description_ NVARCHAR(250) NOT NULL
CONSTRAINT PK_DocumentType PRIMARY KEY (DocumentType_ID)
)

In the above code ‘DocumentType_ID’ column has declared as Identity, having auto increment value by 1 starting with 0. This means the first row inserted will have ‘DocumentType_ID’ value of 1. then 2,3,so on..

In future if we want to reset this value, for what so ever reason, we can use T-SQL:

DBCC CHECKIDENT('Admin.DocumentType_List', RESEED, 0)

This code will reset table Identity back to 0.

Further Reading/Details:http://msdn.microsoft.com/en-us/library/ms176057(SQL.90).aspx

Posted: July 15th, 2009
Categories: T-SQL
Tags: , ,
Comments: No Comments.

Loop through database tables and columns

T-SQL code [Stored Procedure] to loop through all tables and all columns in a database

Applies to MS SQL SERVER 2008

Change [DATABASE_NAME],[SCHEMA_NAME],[USER_NAME] accordingly

Stored Procedure:
USE [DATABASE_NAME]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [SCHEMA_NAME].[loop_Through_Tables_Columns]
AS
BEGIN

SET NOCOUNT ON
DECLARE @TableName nvarchar(256)
DECLARE @ColumnName nvarchar(128)
SET @TableName = ''

WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' /**You can remove this condition to go thourgh with both views and tables**/
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)

PRINT 'TableName: '+ CAST(@TableName AS CHAR)

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
/**You you want to go thourgh with certain type of data types**/
AND DATA_TYPE IN('char','varchar','nchar','nvarchar','text','ntext','image','xml','varbinary')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)
PRINT 'ColumnName: '+ CAST(@ColumnName AS CHAR)
END
END

END
GRANT EXECUTE ON [SCHEMA_NAME].[loop_Through_Tables_Columns] to USER_NAME

Execute Stored Procedure:

EXEC [SCHEMA_NAME].[loop_Through_Tables_Columns]

Posted: July 15th, 2009
Categories: T-SQL
Tags: , , ,
Comments: 1 Comment.

Information Schema Database Metadata

Information schema is part of the SQL-92 standard, holds the structure if your database.

Applies to MS SQL SERVER 2008

T-SQL CODE to see the metada:

SELECT * FROM INFORMATION_SCHEMA.TABLES
SELECT 
TABLE_CATALOG,/*'TABLE_CATALOG' IS SQL-92 STANDARD NAME FOR 'DATABASE'*/
TABLE_SCHEMA,/*'TABLE_SCHEMA' IS SQL-92 STANDARD NAME FOR 'OWNER'*/
TABLE_NAME,
TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES

Further Reading/Details:
http://msdn.microsoft.com/en-us/library/ms186778.aspx

Posted: July 15th, 2009
Categories: T-SQL
Tags: , , ,
Comments: No Comments.

An error occurred when attaching the database

An error occurred when attaching the database – AdvenureWorks database

The below steps applies to MS SQL Server 2008

A. Make sure you enabled database instance to use FILESTREAM as instructed here:
http://msdn.microsoft.com/en-us/library/cc645923.aspx

B. If you downloaded Adventureworks from codeplex, make sure you have set the right user permissions to these files

C. Copy AdventureWorks2008_Data.mdf and AdventureWorks2008_Log.ldf to
‘C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\’
If you see a folder called ‘Documents’ in the downloaded files set, then copy this folder also to ‘C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\’

D. In the managment studio, open new query window and execute the below code: [Change the files path accordingly]

USE [master]
GO
CREATE DATABASE [Adventureworks] ON
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008_Data.mdf' ),
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008_Log.ldf' )
FOR ATTACH
GO
if not exists (select name from master.sys.databases sd where name = N'Adventureworks' and SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() ) EXEC [Adventureworks].dbo.sp_changedbowner @loginame=N'sa', @map=false
GO

OR you can attach a databse either using MS:
1. In SQL Server Management Studio Object Explorer, connect to an instance of the Microsoft SQL Server Database Engine, and then expand that instance.
2. Right click Databases, then Tasks, and then click Attach.
3. In the Attach Databases dialog box, to specify the database to be attached, click Add; and in the Locate Database Files dialog box, select the disk drive where the database resides and expand the directory tree to find and select the .mdf file of the database; for example: C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008_Data.mdf

Optionall : Specify a different name, Change the ownership
4. When you are ready to attach the database, click OK.

Posted: July 14th, 2009
Categories: T-SQL
Tags: , ,
Comments: No Comments.

Search all columns of all tables in a database

Search all columns of all tables in a database

How to search all columns of all tables in a database for a keyword?

Credits to: Vyas

http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm

Here is the complete stored procedure code:

–To search all columns of all tables in Pubs database for the keyword “Computer”

CREATE PROC SearchAllTables
(
 @SearchStr nvarchar(100)
)
AS
BEGIN
<span style="font-family: verdana; color: gray; font-size: xx-small;">
 -- Copyright � 2002 Narayana Vyas Kondreddi. All rights reserved.
 -- Purpose: To search all columns of all tables for a given search string
 -- Written by: Narayana Vyas Kondreddi
 -- Site: http://vyaskn.tripod.com
 -- Tested on: SQL Server 7.0 and SQL Server 2000
 -- Date modified: 28th July 2002 22:50 GMT
</span>
 
 CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
 
 SET NOCOUNT ON
 
 DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
 SET  @TableName = ''
 SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')
 
 WHILE @TableName IS NOT NULL
 BEGIN
 SET @ColumnName = ''
 SET @TableName =
 (
 SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
 FROM 	INFORMATION_SCHEMA.TABLES
 WHERE 		TABLE_TYPE = 'BASE TABLE'
 AND	QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
 AND	OBJECTPROPERTY(
 OBJECT_ID(
 QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
 ), 'IsMSShipped'
        ) = 0
 )
 
 WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
 BEGIN
 SET @ColumnName =
 (
 SELECT MIN(QUOTENAME(COLUMN_NAME))
 FROM 	INFORMATION_SCHEMA.COLUMNS
 WHERE 		TABLE_SCHEMA	= PARSENAME(@TableName, 2)
 AND	TABLE_NAME	= PARSENAME(@TableName, 1)
 AND	DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
 AND	QUOTENAME(COLUMN_NAME) > @ColumnName
 )
 
 IF @ColumnName IS NOT NULL
 BEGIN
 INSERT INTO #Results
 EXEC
 (
 'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
 FROM ' + @TableName + ' (NOLOCK) ' +
 ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
 )
 END
 END
 END
 
 SELECT ColumnName, ColumnValue FROM #Results
END
EXEC SearchAllTables 'Computer'
GO
Posted: July 9th, 2009
Categories: T-SQL
Tags: ,
Comments: No Comments.