Tuesday, September 2, 2014

Error: Either the user does not have access to the database, or the database does not exist





Error: Either the user does not have access to the database, or the database does not exist

Connect to the Analysis Services

Select the Server name and Connect (with the Admin Account) 



Right Click on Server and click Properties




Click the Security and Click Add and add the user which you need and click ok. 



Now try to access the Cube Excel file and the “Error: Either the user does not have access to the database, or the database does not exist” will be gone now. 

Thanks 
Karthikeyan Jothi

Wednesday, March 26, 2014

How to find out the missing foreign keys



I got an assignment to find out the foreign key missing in our database. Our current practices is the Primary Column Name in Master table and the child tables are same. I have attached the example how we use to create the tables and how to find out the missing foreign keys. This Scripts can be changed according to your project requirement...

------------------------------------------------------------------------------------------------------------------------------------
USE tempdb
GO

------------------------------------------------------------------------------------------------------------------------------------
-- Just for testing created 2 tables

    CREATE TABLE TB_ORDER_MASTER (ORDER_MASTER_ID INT CONSTRAINT PK_ORDER_MASTER_ID PRIMARY KEY, ORDER_NAME VARCHAR(100))
    GO
    CREATE TABLE TB_ORDER_DETAILS (ORDER_DETAILS_ID INT CONSTRAINT PK_ORDER_DETAILS_ID PRIMARY KEY, ORDER_MASTER_ID INT, ORDER_DATE DATETIME)
    GO
------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------
-- Get the Tables which Contains Primary Key

    IF OBJECT_ID('tempdb..#temp_PK','U') IS NOT NULL DROP TABLE tempdb.dbo.#temp_PK
    SELECT  PK.name AS CONSTRAINT_NAME,
            TBL.name AS TABLE_NAME,
            COL.name AS COLUMN_NAME
    into #temp_PK
    FROM sys.key_constraints as PK
        INNER JOIN sys.tables as TBL
                ON TBL.object_id = PK.parent_object_id
        INNER JOIN sys.index_columns as IDXCOL
                ON IDXCOL.object_id = TBL.object_id
                AND IDXCOL.index_id = PK.unique_index_id
        INNER JOIN sys.columns as COL
                ON COL.object_id = TBL.object_id
                AND COL.column_id = IDXCOL.column_id
    WHERE
        PK.type = 'PK'
      
    select * from #temp_PK  

/*
--Output

CONSTRAINT_NAME        TABLE_NAME            COLUMN_NAME
PK_ORDER_MASTER_ID    TB_ORDER_MASTER        ORDER_MASTER_ID
PK_ORDER_DETAILS_ID    TB_ORDER_DETAILS    ORDER_DETAILS_ID

*/
------------------------------------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------------------

-- Get the Foreig Key columns and details.
    IF OBJECT_ID('tempdb..#temp_FK','U') IS NOT NULL DROP TABLE tempdb.dbo.#temp_FK
    SELECT
            TBL.name AS TABLE_NAME  ,
            COL.name AS COLUMN_NAME
    INTO    #temp_FK
    FROM sys.foreign_key_columns FKC
        INNER JOIN sys.sysobjects OBJ
                ON FKC.constraint_object_id=OBJ.id
        INNER JOIN sys.sysobjects TBL
                ON FKC.parent_object_id=TBL.id
        INNER JOIN sys.all_columns COL
                ON FKC.parent_object_id=COL.object_id /* ID of the object to which this column belongs.*/
                AND FKC.parent_column_id=COL.column_id/* ID of the column. Is unique within the object.Column IDs might not be sequential.*/

    select * from #temp_FK

    /*
    --Output
    TABLE_NAME        COLUMN_NAME  
    NULL            NULL
    */
------------------------------------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------------------
-- GET THE RESULT FOR MISSING FOREIGN KEYS
SELECT    ZZZ.COLUMN_NAME MISSING_FK_COLUMN_NAME, ZZZ.TABLE_NAME MISSING_FK_TABLE_NAME,
        PK.TABLE_NAME PK_TABLE_NAME
  FROM
        (
            SELECT *
              FROM
                (
                         SELECT CLM.COLUMN_NAME, CLM.TABLE_NAME
                           FROM INFORMATION_SCHEMA.COLUMNS CLM JOIN
                                #TEMP_PK PK
                                    ON CLM.COLUMN_NAME = PK.COLUMN_NAME -- Join the PK Column to get all the columns with named as Primary Key
                  
                    -- To Avoid the Actual Primary Keys
                    EXCEPT
                  
                        SELECT COLUMN_NAME, TABLE_NAME
                          FROM #TEMP_PK
                ) X

            EXCEPT

             SELECT COLUMN_NAME, TABLE_NAME
               FROM #TEMP_FK
        ) ZZZ JOIN
          #TEMP_PK PK
            ON ZZZ.COLUMN_NAME = PK.COLUMN_NAME

    ORDER BY ZZZ.COLUMN_NAME, ZZZ.TABLE_NAME


/*
--Output
MISSING_FK_COLUMN_NAME    MISSING_FK_TABLE_NAME    PK_TABLE_NAME
ORDER_MASTER_ID            TB_ORDER_DETAILS        TB_ORDER_MASTER
*/
------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------
-- Now we got the list and create the Foreign Keys
ALTER TABLE TB_ORDER_DETAILS ADD CONSTRAINT FK_ORD_DET_X_ORDER_MASTER_ID_X_ORD_MAS FOREIGN KEY(ORDER_MASTER_ID)
 REFERENCES TB_ORDER_MASTER(ORDER_MASTER_ID)
GO

------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------
DROP TABLE TB_ORDER_MASTER
GO
DROP TABLE TB_ORDER_DETAILS
GO

------------------------------------------------------------------------------------------------------------------------------------

Create a Databse only with mdf file



Step 1 – Right click and select the attach the database.

Step 2 – Select the MDF file.





Step 3 – Select the Log file and click Remove.




Step 4 – Click Ok


The database is ready for use. Sql Server automatically creates the LDF file.

Monday, January 13, 2014

Error: 18452 Login failed for user ‘(null)’. The user is not associated with a trusted SQL Server connection.

Fix/Solution/Workaround:

Change the Authentication Mode of the SQL server from “Windows Authentication Mode (Windows Authentication)”
to “Mixed Mode (Windows Authentication and SQL Server Authentication)”.

Run following script in SQL Analyzer to change the authentication

ALTER LOGIN sa ENABLE
GO
ALTER LOGIN sa WITH PASSWORD = '<password>'
GO

OR

In Object Explorer, expand Security, expand login, right-click on sa, and then click Properties. On the General page, you may have to create and confirm a password for the sa login. On the Status page, in the Login section, click Enabled, and then click OK.

Using the Alias name instead of IP address/Host Name/Instance Name for accessing the SQL Server


When working on Support and Production DBA there is the possibility we need to access the multiple SQL Server Instance. Most of the time we will be using the IP address/Host Name/Instance Name. Just thought of checking whether the SQL Server is allowing us to use the Alias name instead of using the IP address. It would be very helpful and we can access the server with our own creative and easy accessible name.

Especially when you are in support (or) managing multiple server it would be easy to remember the Alias name instead of IP address.

Steps:
  • Click on SQL Server Configuration Manager
  • Then Click on Alias Name


  • Right Click and add new Alias


 
  • Enter the following info and click Ok.
    • Alias Name (The Name you would like to keep for that IP address/Host Name/Instance Name)
    • Post No (Default 1433) - (OR) Provide the Port number which used was used in Server.
    • Protocol – TCP/IP
    • Server – (It can be Actual IP address/Host Name/Instance Name)






  • Now you can access the Database Server using the New Alias Name in SQL Server Management Studio.


Thanks & Regards,
Karthikeyan Jothi.
"You Never Fail Until You Stop Trying"

Get the file name from given path



Get the file name from given path

Steps:
·         Copy the above below script and Execute the full script....
·         Run the below given script

Execute:
SELECT dbo.UDF_GET_SPECIFIC_STRING('c:\testing\name.txt')
-- The output will be "name.txt"


IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'UDF_GET_FILE_NAME_FROM_GIVEN_PATH' AND TYPE = 'FN')
    DROP FUNCTION UDF_GET_FILE_NAME_FROM_GIVEN_PATH
GO

CREATE FUNCTION [dbo].[UDF_GET_FILE_NAME_FROM_GIVEN_PATH]
(@PS_String VARCHAR(1000))
RETURNS VARCHAR(256)
AS
/*       
----------------------------------------------------------------------------------------------------------------------------------------------------------------
    Description            :
                       Get the file name from given path
                     
    Input Parameter        : TEXT
                         
                 
    Return Value        : String

    --IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'UDF_GET_FILE_NAME_FROM_GIVEN_PATH' AND TYPE = 'FN')
    --    DROP FUNCTION UDF_GET_FILE_NAME_FROM_GIVEN_PATH
    --GO
----------------------------------------------------------------------------------------------------------------------------------------------------------------
    --Created By    : Karthikeyan Jothi
    --Created On    : 1/10/2014

    --USP : User Defined Function

    --- Changes Details
    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    --S.no    Modified By            Modified On        Reason
    ------------------------------------------------------------------------------------------------------------------------------------------------------------

    -- Execution Part
    --====================
        Run the UDF with different test values
        SELECT dbo.UDF_GET_FILE_NAME_FROM_GIVEN_PATH('') AS 'EmptyString';
        O/P -> NULL
     
        SELECT dbo.UDF_GET_FILE_NAME_FROM_GIVEN_PATH('c:\testing\name.txt')
        O/P -> name.txt
     
        SELECT dbo.UDF_GET_FILE_NAME_FROM_GIVEN_PATH('c:\testing\tetsubfdsasbakjskanbsakbskas\eg.txt')
        O/p -> eg.txt
 
    --***********************************--
----------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
BEGIN
Declare     @output varchar(256)

    -- Best Way...
    IF LEN(@PS_String)> 0
    BEGIN
    SET @output = RIGHT( @PS_String, CHARINDEX( '\', REVERSE( @PS_String)) - 1)
    END
  
    return @output
END-------------------------------------------------------------------------------------------------------------------------


Thanks & Regards,
Karthikeyan Jothi.
"You Never Fail Until You Stop Trying"