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

Tuesday, 9 October 2018

SQL NOT LIKE will not applicable for NULL values

Example:
select * from emp WHERE Name NOT LIKE '%ABC%' --will return only where Name not like %ABC%, but will not return where Name is NULL

select * from emp WHERE (Name NOT LIKE '%ABC%' OR Name IS NULL) --will return only where Name not like %ABC% and will return where Name is NULL

Friday, 23 June 2017

Copy SQL tables in to new data base



I have a SQL DB name Test
table names are Tbl_1, Tbl_2
Now i would like to create new DB with same tables from Test

Create new DB Test_Copy
Create new query from Test_Copy DB
Now run below query to create tables with data

 select * into [dbo].[Tbl_1] from [Test].[dbo].[Tbl_1]
 select * into [dbo].[Tbl_2] from [Test].[dbo].[Tbl_2]

Sunday, 25 December 2016

Find a string by searching all tables in SQL Server


CREATE PROC SearchAllTables
(
@SearchStr nvarchar(100)
)
AS
BEGIN

    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', 'int', 'decimal')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
            )

            IF @ColumnName IS NOT NULL

            BEGIN
                INSERT INTO #Results
                EXEC
                (
                    'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
                    FROM ' + @TableName + 'WITH (NOLOCK) ' +
                    ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                )
            END
        END  
    END

    SELECT ColumnName, ColumnValue FROM #Results
END

Then call store proc
exec SearchAllTables '843DE4FEAF494C59A656B4CD4AAB7669'

then delete  store proc and temp tables

Tuesday, 17 May 2016

How can I get column names from a table in SQL Server?

SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('dbo.yourTableName')


or

  SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
     TABLE_NAME  ='MyTable'
      AND
      TABLE_CATALOG='MyDBName'

Thursday, 12 November 2015

sql multiple inner joins to same table



CREATE TABLE Table1
([weddingtable] int, [tableseat] int, [tableseatid] int, [name] varchar(4),
    [Created] int,[Modified] int)
;

INSERT INTO Table1
([weddingtable], [tableseat], [tableseatid], [name],[Created],[Modified[)
VALUES
(001, 001, 001001, 'bob',1,2),
(001, 002, 001002, 'joe',2,3),
(001, 003, 001003, 'dan',2,2),
(002, 001, 002001, 'mark',3,1)
;

CREATE TABLE Table2
([weddingtable] int, [tableseat] int, [meal] varchar(7))
;

INSERT INTO Table2
([weddingtable], [tableseat], [meal])
VALUES
(001, 001, 'chicken'),
(001, 002, 'steak'),
(001, 003, 'salmon'),
(002, 001, 'steak')
;
 
 CREATE TABLE usert
([userID] int,  [userName] varchar(7))
;
                                                                         
INSERT INTO usert
([userID], [userName])
VALUES
(1, 'A'),
(2, 'B'),
(3, 'C')
;
       
-----------------------
--Query

select * from Table1 t1
inner join usertable u
on t1.cre=u.userID
inner join usertable u1
on t1.Mod=u1.userID                                                                   

Sunday, 20 September 2015

Not able to connect Integration Services through SSMS


Solution:
To grant access to the Integration Services service

Run Dcomcnfg.exe. Dcomcnfg.exe provides a user interface for modifying certain settings in the registry.
In the Component Services dialog, expand the Component Services > Computers > My Computer > DCOM Config node.
Right-click Microsoft SQL Server Integration Services 11.0, and then click Properties.
On the Security tab, click Edit in the Launch and Activation Permissions area.
Add users and assign appropriate permissions, and then click Ok.
Repeat steps 4 - 5 for Access Permissions.
Restart SQL Server Management Studio.
Restart the Integration Services Service.

Wednesday, 26 August 2015

Column ' ' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;




You are expecting one  column in select , but you are not adding at GROUP BY