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

2014/04/15

Look up table with column name (SQL)

select a.name
from sysobjects a, syscolumns b
where a.id = b.id
and b.name = 'CFRMN_IND'

Look up table with partial table name (SQL)

select top 50 * from GTDMAST1..sysobjects
where type = 'U'
and name like "%SCA%"

What is sp_lock & sp_who (SQL)

sp_lock (Reports information about processes that currently hold locks.)
sp_who (Run sp_who to get the spid of the locking process.)

2010/06/10

Enabling xp_cmdshell in SQL Server

EXEC master.dbo.sp_configure 'show advanced options', 1

RECONFIGURE

EXEC master.dbo.sp_configure 'xp_cmdshell', 1

RECONFIGURE

2010/05/04

SSIS Deployment to SQL Server

SSIS Deployment to SQL Server


1) Go to SSIS project folder\bin\, double click the DeploymentManifest file
2) Choose “SQL Server deployment”.
3) Fill in the information then you are done.

2010/04/21

SQL - How to reset identity increment value is MSSQL?

run the script below...BRAVO!

DBCC CHECKIDENT ( #table_name# ,RESEED, #number_starting_from#)

#table_name# = the table you wish to reset the identity increment
#number_starting_from# = the number you wish to reset to. For example, if you wish to start your new item at 1, then put in 0, like the example below:

DBCC CHECKIDENT ( my_table ,RESEED, 0)

Enjoy!

2010/04/09

Enabling the table for SQL Cache Dependency

To enable the table for SQL Cache dependency

First go to c:\WINDOWS\Microsoft.NET\Framework\versionNumber
Then type in the following command:
aspnet_regsql -S [DB server] -U [user name] -P [password] -d [DB name] -ed -et -t [table]

2009/10/07

SQL- Split string function

Create this function:
CREATE FUNCTION dbo.Split(@String nvarchar(4000), @Delimiter char(1))
returns @Results TABLE (Items nvarchar(4000))
as
begin
declare @index int
declare @slice nvarchar(4000)

select @index = 1
if @String is null return

while @index != 0

begin
select @index = charindex(@Delimiter,@String)
if @index !=0
select @slice = left(@String,@index - 1)
else
select @slice = @String

insert into @Results(Items) values(@slice)
select @String = right(@String,len(@String) - @index)
if len(@String) = 0 break
end return
end


you can try the line below:

select * from dbo.split('a,b,c,d,e,f,g,h,i,j,k,l', ',')

SQL- clone table instantly (SQL)

Sometimes we want to clone a table. How to do it quick? Look at the code below:

select t.* into [#temp]
from TABLENAME t
where 1 = 0

The above code will create a temp table [#temp] with the same properties of table TABLENAME. Some of you might get confused why there is a line "where 1 = 0" what is that means? Actually this means that we do not want to clone the data, we just want to clone the table. So in here 1 = 0 will always false and so there will be no data insert into the temp table.

Enjoy

2009/09/14

SQL - check if primary key exists in a table

IF EXITS (SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TABLE_NAME = '..... your table name ....'
AND TABLE_SCHEMA ='dbo' )
BEGIN

---- Your code here

SQL - create stored procedure if not exists

IF NOT EXISTS(Select * from sysobjects where name = 'usp_UserSproc')
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE usp_UserSproc
(
@ID int = NULL --nullable input
)
AS
BEGIN
-- BLAH BLAH BLAH
END';
END

2009/08/25

MSSQL- use runas to login to sql server with windows authentication

For some situation, you will need to use remote computer to connect to different server and login the sql server by windows authentication. But good news, you can use the following technique so that you can just login to the specific sql server from local machine.

- Open cmd
- Type:
runas /user: domain\loginname "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe"

or create a short cut with the following path:
C:\WINNT\system32\runas.exe /user:domain\login "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe"

So you save your time by without having to connect to your remote computer ;)

2009/08/07

SQL- Stored procedure: list out all columns of a table

Sometimes, you need to create a temp table or whatever, you need to list out all the columns. If just only 2 or 3 columns sure that won't bother you, but if the table has 100 columns, its gonna kill you if you have to type everyone of it. So, here is the stored procedure for you. It will list out all columns of a table in one line and separate the columns with comma.


Usage: exec SP_ColName 'TABLENAME'

===
IF EXISTS (SELECT * FROM [sysobjects] WHERE [id] = OBJECT_ID(N'[dbo].[SP_ColName]'))
DROP PROCEDURE [SP_ColName]
GO
-- Usage: SP_ColName
-- It prints out the column names of the given table in one line delimited by ', '
CREATE PROCEDURE [SP_ColName] @TableName sysname
AS
BEGIN
DECLARE @ColumnList nvarchar(max)
DECLARE @ColumnName nvarchar(max)
SET @ColumnList = ''
DECLARE cur CURSOR FOR
SELECT [name] FROM [syscolumns] WHERE [id] = OBJECT_ID(@TableName) ORDER BY [colid]
OPEN cur
DECLARE @OutStr varchar(max)
SELECT @OutStr = ''
PRINT UPPER(@TableName)
FETCH NEXT FROM cur INTO @ColumnName
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @OutStr = @OutStr + @ColumnName + ', '
FETCH NEXT FROM cur INTO @ColumnName
END
IF LEN(@OutStr) >= 2 PRINT LEFT(@OutStr,LEN(@OutStr)-2)
CLOSE cur
DEALLOCATE cur
END
GO

2009/08/04

SQL- List out all the columns in a table

Assume that you wish to check for rows in a table where all values are NULL. There are many columns in the table and you want to avoid hand coding them. Instead, you can create a script to do the work for you:

Select column_name + ' IS NULL AND'
FROM INFORMATION_SCHEMA.columns
where table_name = 'whatever'

Enjoy! this query is very useful ;)

2009/05/27

Get windows login name?

To get windows login name, you can type this in dos prompt:
echo %username%

To get windows login name in sql, you can type this:
exec master..xp_cmdshell 'echo %USERNAME%'