Showing posts with label MS SQL-Server. Show all posts
Showing posts with label MS SQL-Server. Show all posts

Wednesday, January 15, 2020

SQL Server - Running large Sql files

When we try to open a large Sql file in SQl Server, it's gives the error
"The operation could not be completed. not enough storage is available to complete this operation
The template specified cannot be found. Please check that the full path is correct"

Solution:

use the sqlcmd tool to execute the file.

To execute a SQL script:
  1. Start the Command Prompt
  2. Run the below command  

sqlcmd -S DatabaseServer -d Database -i C:\Users\Administrator\Downloads\script.sql -x


Tuesday, January 14, 2020

Error "A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe"

Error "A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe"


Solution:
This is login authenticate problem. Please check the following things:

  • In the SQL Server Properties, please check that "SQL Server and Windows Authentication Mode" is enabled, if not enable, please enable and restart the SQL Server.
     
  • Is Login have the permission to particular Database?
     
  • This might be a firewall issue as you mentioned it only happens when security agent installed. Please make sure you have properly configured security agent to allow Database Engine Services accesses.
     


Thursday, April 11, 2019

Microsoft SQL Server - How to check all empty tables in database?

Process to check all empty tables in a Database:

1. Select the database in the SSMS and open the "New Query" window.
2. Run the below query.

SELECT sch.name as SchemaName, t.NAME AS TableName,  p.rows AS RowCounts
FROM   sys.tables t
INNER JOIN  sys.partitions p ON t.object_id = p.OBJECT_ID
INNER JOIN sys.schemas sch on t.schema_id = sch.schema_id
WHERE  t.NAME NOT LIKE 'dt%'   AND t.is_ms_shipped = 0   AND p.rows <=0
GROUP BY  sch.name,t.Name, p.Rows
ORDER BY  sch.name,t.Name

Friday, April 5, 2019

System.InvalidOperationException: 'String[n]: the Size property has an invalid size of 0.'


This error occurs generally when we are not passing the Output parameter in the SqlDbType.VarChar value like the below example

SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar);
job1.Direction = ParameterDirection.Output;

You need to define a length when specifying SqlDbType.VarChar parameter, So

SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar, 50);
You should use the same length as defined in your SQL Server stored procedure.

Thursday, March 14, 2019

Ms Sql Server - The Visual Studio Component is out of date. Please restart Visual Studio. (Mscorlib)

SSMS Error: "The Visual Studio Component is out of date. Please restart Visual Studio. (Mscorlib)"

When you are trying to open Query or execute query in Microsoft SQL Server Management Studio, you may see the following message:

The Visual Studio Component is out of date. Please restart Visual Studio. (Mscorlib).

Cause: Sometimes cache of the Sql Server corrupted due to Updates in the visual studio or Sql-Server components updates.

Solution : 

STEP 1: Delete the Temp Files. Shortcut to delete the temp files


STEP 2: Restart Microsoft SQL Server Management Studio and the error message will have disappeared.



A network-related or instance-specific error occurred while establishing a connection to SQL Server - microsoft sql server error 2


SSMS Error: "A network-related or instance-specific error occurred while establishing a connection to SQL Server"

When you are trying to connect to a SQL Server using Microsoft SQL Server Management Studio, you may see the following message:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that the SQL Server is configured to allow remote connections. (provided: Named Pipes Provider, error: 40- Could not open a connection to the SQL Server) (Microsoft SQL Server, Error: 2).

While the message tells you to check some of the items that can cause this issue, it fails to mention that if your SQL Server Service is not started, then you cannot connect. One of the first things that you should check is that the SQL Server (MSSQLSERVER) is started. You can go to the Services Console (services.msc) and look for SQL Server (MSSQLSERVER) to see that it is started. If not, then start the service.

You could also do this through an elevated command prompt by typing net start mssqlserver.


Tuesday, February 26, 2019

Sql Server - Create a copy of database and Renaming it

Steps to attach a database with different name:

  1. Detach database from local PC
  2. Copy the mdf and ldf files and rename them.
  3. Copy\Move the files to the server directory where the original Database is stored.
  4. Use the below code
USE [master]
GO
CREATE DATABASE [example_db] ON ( FILENAME = N'D:\Data\example_db.mdf' ),
( FILENAME = N'E:\Logs\example_db_log.ldf' )FOR ATTACH
GO


you can also copy by using the Sql-server Management studio:

  • go to database and tasks->Copy Database
  • Select the source and destination server (same if you wants to make a copy on the same server)
  • change the destination database name to required.
  • Done

Tuesday, January 1, 2019

How to transfer logins and passwords between SQL Server

Transfer logins and passwords to destination server (Server A) using scripts generated on source server (Server B)

To create a script on the source Server, follow these steps:

  1. On server A, start SQL Server Management Studio, and then connect to the instance of SQL Server from which you moved the database.
  2. Open a new Query Editor window, and then run the following script.
    USE master
    GO
    IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL
      DROP PROCEDURE sp_hexadecimal
    GO
    CREATE PROCEDURE sp_hexadecimal
        @binvalue varbinary(256),
        @hexvalue varchar (514) OUTPUT
    AS
    DECLARE @charvalue varchar (514)
    DECLARE @i int
    DECLARE @length int
    DECLARE @hexstring char(16)
    SELECT @charvalue = '0x'
    SELECT @i = 1
    SELECT @length = DATALENGTH (@binvalue)
    SELECT @hexstring = '0123456789ABCDEF'
    WHILE (@i <= @length)
    BEGIN
      DECLARE @tempint int
      DECLARE @firstint int
      DECLARE @secondint int
      SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
      SELECT @firstint = FLOOR(@tempint/16)
      SELECT @secondint = @tempint - (@firstint*16)
      SELECT @charvalue = @charvalue +
        SUBSTRING(@hexstring, @firstint+1, 1) +
        SUBSTRING(@hexstring, @secondint+1, 1)
      SELECT @i = @i + 1
    END
    
    SELECT @hexvalue = @charvalue
    GO
     
    IF OBJECT_ID ('sp_help_revlogin') IS NOT NULL
      DROP PROCEDURE sp_help_revlogin
    GO
    CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS
    DECLARE @name sysname
    DECLARE @type varchar (1)
    DECLARE @hasaccess int
    DECLARE @denylogin int
    DECLARE @is_disabled int
    DECLARE @PWD_varbinary  varbinary (256)
    DECLARE @PWD_string  varchar (514)
    DECLARE @SID_varbinary varbinary (85)
    DECLARE @SID_string varchar (514)
    DECLARE @tmpstr  varchar (1024)
    DECLARE @is_policy_checked varchar (3)
    DECLARE @is_expiration_checked varchar (3)
    
    DECLARE @defaultdb sysname
     
    IF (@login_name IS NULL)
      DECLARE login_curs CURSOR FOR
    
          SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM 
    sys.server_principals p LEFT JOIN sys.syslogins l
          ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name <> 'sa'
    ELSE
      DECLARE login_curs CURSOR FOR
    
    
          SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM 
    sys.server_principals p LEFT JOIN sys.syslogins l
          ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = @login_name
    OPEN login_curs
    
    FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
    IF (@@fetch_status = -1)
    BEGIN
      PRINT 'No login(s) found.'
      CLOSE login_curs
      DEALLOCATE login_curs
      RETURN -1
    END
    SET @tmpstr = '/* sp_help_revlogin script '
    PRINT @tmpstr
    SET @tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */'
    PRINT @tmpstr
    PRINT ''
    WHILE (@@fetch_status <> -1)
    BEGIN
      IF (@@fetch_status <> -2)
      BEGIN
        PRINT ''
        SET @tmpstr = '-- Login: ' + @name
        PRINT @tmpstr
        IF (@type IN ( 'G', 'U'))
        BEGIN -- NT authenticated account/group
    
          SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + @defaultdb + ']'
        END
        ELSE BEGIN -- SQL Server authentication
            -- obtain password and sid
                SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, 'PasswordHash' ) AS varbinary (256) )
            EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT
            EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT
     
            -- obtain password policy state
            SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
            SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
     
                SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' WITH PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + @SID_string + ', DEFAULT_DATABASE = [' + @defaultdb + ']'
    
            IF ( @is_policy_checked IS NOT NULL )
            BEGIN
              SET @tmpstr = @tmpstr + ', CHECK_POLICY = ' + @is_policy_checked
            END
            IF ( @is_expiration_checked IS NOT NULL )
            BEGIN
              SET @tmpstr = @tmpstr + ', CHECK_EXPIRATION = ' + @is_expiration_checked
            END
        END
        IF (@denylogin = 1)
        BEGIN -- login is denied access
          SET @tmpstr = @tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( @name )
        END
        ELSE IF (@hasaccess = 0)
        BEGIN -- login exists but does not have access
          SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( @name )
        END
        IF (@is_disabled = 1)
        BEGIN -- login is disabled
          SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME( @name ) + ' DISABLE'
        END
        PRINT @tmpstr
      END
    
      FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
       END
    CLOSE login_curs
    DEALLOCATE login_curs
    RETURN 0
    GO
    Note This script creates two stored procedures in the master database. The procedures are named sp_hexadecimal and sp_help_revlogin.
  3. Run the following statement in the same or a new query window:

    EXEC sp_help_revlogin

    The output script that the sp_help_revlogin stored procedure generates is the login script. This login script creates the logins that have the original Security Identifier (SID) and the original password.

Steps on the destination server (Server B):


  • On server B, start SQL Server Management Studio, and then connect to the instance of SQL Server to which you moved the database.
  • Open a new Query Editor window, and then run the output script that's generated in step 2 of the preceding procedure.

Thursday, December 24, 2009

MS SQL-Server, Recover Database from suspect Mode

Steps to recover MS SQL-server Database from "suspect "mode:
EXEC sp_resetstatus 'DatabaseName';
ALTER DATABASE DatabaseName SET EMERGENCY
DBCC checkdb('DatabaseName')
DBCC CHECKCATALOG('DatabaseName')
ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ('DatabaseName', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE DatabaseName SET MULTI_USER
ALTER DATABASE DatabaseName SET ONLINE;