Thursday, November 12, 2015

Determine Whether a Table Has an Identity Column

SQL Server 2005+

SELECT OBJECTPROPERTY(object_id('TableToBeChecked'), 'TableHasIdentity')

SMO

Server srv = new Server("(local)");
Database db = srv.Databases["AdventureWorks2012"];

Table tb = new Table(db, "Test Table");
Column col1 = new Column(tb, "TableIdentifier", DataType.Int);
col1.Identity = true;
tb.Columns.Add(col1); 
try
{
foreach (String s in tb.CheckIdentityValue())
{
   Console.WriteLine(s);
}
}
catch
{
}

Wednesday, October 28, 2015

How to find a column in all tables sql server

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE 'BranchId'
ORDER BY schema_name, table_name;

Saturday, October 10, 2015

bobj is not defined crystal report

Make sure aspnet_client folder is present in the root of your application with the appropriate version of asp net framework
for dot net 4.0, 4.5.* use folder aspnet_client\system_web\4_0_30319\crystalreportviewers13
for dot net 4.6 use folder aspnet_client\system_web\4_6_81\crystalreportviewers13

Saturday, October 3, 2015

Variable table name ms sql server

no allowed use dynamic sql instead.

build dynamic sql and then use exec or sp_executesql to run query

EXEC | EXECUTE

Example
DECLARE @table VARCHAR(50) = 'HR.Employees';
EXEC ( 'SELECT * FROM '+@table );

Example 2 [Stored Procedure]
DECLARE @ID INT = 34;
EXEC ( '[dbo].[p__Clean] '''+@ID+'''' );

Friday, October 2, 2015

How to determine the number of days in a month in SQL Server?

CREATE FUNCTION dbo.f__GetDaysInMonth(
                @Date DATE )
RETURNS TINYINT
AS
BEGIN
     DECLARE @Days TINYINT;
     SELECT @Days = DAY(DATEADD(month, DATEDIFF(MONTH, 0, @Date) + 1, -1));
 RETURN @Days
END;
GO
--Usage
SELECT dbo.f__GetDaysInMonth('11-Apr-2015')