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 ;)

Reporting Services- Thousand separator

With decimal:
Format(First(Fields!IRTotal.Value, "TDec"), "#,###0.00")

Without decimal:
Format(First(Fields!IRTotal.Value, "TDec"), "#,###0")

2009/08/21

Reporting Services- Error [BC30494]

I am working on Report and I got this error:

error [BC30494] Line is too long.

Problem\Solution: If your table is too big will causes this error. There is a simple solution: rename all your text boxes to a short name, for example: textbox1234 - > t1234

If the above solution doesn't help, try to split the table into two.

Enjoy

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

C# Is null or string is empty

I just found that there is a "new" thing to check the string is null or empty (asp.net 2.0)

old way:
if (var == string.Empty && var == null)

new way:
if (String.IsNullOrEmpty(var))

It looks cool right? Enjoy!