Monday, May 21, 2012

Javascript Math Round



The required number argument is the value to be rounded to the nearest integer.
For positive numbers, if the decimal portion of number is 0.5 or greater, the return value is equal to the smallest integer greater than number. If the decimal portion is less than 0.5, the return value is the largest integer less than or equal to number.
For negative numbers, if the decimal portion is exactly -0.5, the return value is the smallest integer that is greater than the number.
For example, Math.round(8.5) returns 9, but Math.round(-8.5) returns -8.


MSDN Ref LINK


For Example :

var a=Math.round(2.60);      [3]
var b=Math.round(2.50);      [3]
var c=Math.round(2.49);      [2]
var d=Math.round(-2.60);    [-3]
var e=Math.round(-2.5);      [-2]
var f=Math.round(-2.49);     [-2]








Thursday, May 17, 2012

Script All the Stored Procedures in The Database


Script All the Stored Procedures in The Database

In Sql Server 2005 and 2008 you can script the stored procedure in Management Studio by right clicking on Store Procedure name and clicking on “Script Store Procedure as” and then “Create To”.
But if you want to script all the Stored Procedures in the database programmatically, then here is the simple T-SQL query for it -
To script All the Stored Procedures in the Database :
SELECT    O.Name as ProcName
        ,M.Definition as CreateScript
        ,O.Create_Date
        ,O.Modify_Date
FROM sys.sql_modules as M INNER JOIN sys.objects as O
ON M.object_id = O.object_id
WHERE O.type = 'P'
If the Stored Procedure is created with ENCRYPTION option then you will get the NULL in the definition column.
Similarly,
To script All the Views in the Database :
SELECT    O.Name as ProcName
        ,M.Definition as CreateScript
        ,O.Create_Date
        ,O.Modify_Date
FROM sys.sql_modules as M INNER JOIN sys.objects as O
ON M.object_id = O.object_id
WHERE O.type = 'V'
To script All the Functions in the Database :
SELECT    O.Name as ProcName
        ,M.Definition as CreateScript
        ,O.Create_Date
        ,O.Modify_Date
FROM sys.sql_modules as M INNER JOIN sys.objects as O
ON M.object_id = O.object_id
WHERE O.type = 'FN'
For scripting all Triggers small modification is required, instead of sys.objects I joined thesys.triggers with sys.sql_modules.
To script All the Triggers in the Database :
SELECT    O.Name as ProcName
        ,M.Definition as CreateScript
        ,O.Create_Date
        ,O.Modify_Date
FROM sys.sql_modules as M INNER JOIN sys.triggers as O
ON M.object_id = O.object_id
Resource Reference Link

Friday, April 27, 2012

Multithread Pass List Parameter to Function

 ThreadPool.QueueUserWorkItem(new WaitCallback(Function), new object[] { param1,  param2, param3, param4 });

Thursday, April 26, 2012

Scripting dependency of User Defined Table Types


-- Find all referencing objects to user-defined table type in @fullObjectName parameter
-- and generate DROP scripts and CREATE scripts for them
CREATE PROC ap_FindReferences (@fullObjectName VARCHAR(200))
AS
BEGIN
    SET NOCOUNT ON

    IF (TYPE_ID (@fullObjectName) IS NULL)
    BEGIN
        RAISERROR ('User-defined table type ''%s'' does not exists. Include full object name with schema.', 16,1, @fullObjectName)
        RETURN
    END;

    WITH sources
    AS
    (
        SELECT ROW_NUMBER() OVER (ORDER BY OBJECT_NAME(m.object_id)) RowId, definition
        FROM sys.sql_expression_dependencies d
        JOIN sys.sql_modules m ON m.object_id = d.referencing_id
        JOIN sys.objects o ON o.object_id = m.object_id
        WHERE referenced_id = TYPE_ID(@fullObjectName)
    )

    SELECT 

        'DROP ' +
            CASE OBJECTPROPERTY(referencing_id, 'IsProcedure')
            WHEN 1 THEN 'PROC '
            ELSE
                CASE
                    WHEN OBJECTPROPERTY(referencing_id, 'IsScalarFunction') = 1 OR OBJECTPROPERTY(referencing_id, 'IsTableFunction') = 1 OR OBJECTPROPERTY(referencing_id, 'IsInlineFunction') = 1 THEN 'FUNCTION '
                    ELSE ''
                END
            END
        + SCHEMA_NAME(o.schema_id) + '.' +
        + OBJECT_NAME(m.object_id)    

    FROM sys.sql_expression_dependencies d
    JOIN sys.sql_modules m ON m.object_id = d.referencing_id
    JOIN sys.objects o ON o.object_id = m.object_id
    WHERE referenced_id = TYPE_ID(@fullObjectName)
    UNION  ALL
    SELECT  'GO'
    UNION  ALL
    SELECT
        CASE
            WHEN number = RowId    THEN DEFINITION
            ELSE 'GO'
        END
     FROM sources s
    JOIN (SELECT DISTINCT number FROM master.dbo.spt_values) n ON n.number BETWEEN RowId AND RowId+1

END
GO

-- Invokes ap_FindReferences procedure and writes scripted result to .sql file 
CREATE PROC ap_WriteReferences
@typeToFind VARCHAR(200)
AS
BEGIN

    DECLARE @sqlCmd VARCHAR(500)
    DECLARE @database VARCHAR(200) = 'test'
    DECLARE @outputFile VARCHAR(500) = 'c:\refences.sql'

    SET @sqlCmd = 'sqlcmd.exe -d '+@database+' -q "EXEC ap_FindReferences '''+ @typeToFind +'''" -o '+ @outputFile +' -h-1 -y0'

    EXEC xp_cmdshell @sqlCmd

END

How To Enable Xp_CmdShell in Sql Server?


How To Enable Xp_CmdShell in Sql Server?

Today I will show you how to enable the Xp_CmdShell extended stored procedure in Sql Server 2005 and 2008.
Well normal error message you'll get when Xp_CmdShell is not enabled on your Sql Server and you to try execute some Windows commands using Xp_CmdShell is

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.



You can enable the Xp_CmdShell using 2 ways, either by executing T-sql statements or from
"Surface Area Configuration Manager". We'll see both of them.



A. Enable Xp_Cmdshell from Management Studio.
For enabling Xp_CmdShell from Management Studio you need to execute following code.


-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO-- To update the currently configured value for advanced options.RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO


B. Enable the Xp_CmdShell from "Surface Area Configuration Manager."
1. Click Start, point to Programs, point to Microsoft SQL Server 2005, point toConfiguration Tools, and then click SQL Server Surface Area Configuration.
2. On the SQL Server 2005 Surface Area Configuration page, click Surface Area Configuration for Features.

3. Click on xp_cmdshell and tick on checkbox of "Enable the xp_cmdshell".
 
 
Ref : http://mangalpardeshi.blogspot.com/2008/12/how-to-enable-xpcmdshell-stored.html 

Sunday, April 15, 2012

助你成为百万富翁的10句箴言

几十年来我总结出10句箴言,可以帮助你成为美国的下一位百万富翁:

1. 不要光盯着钱看

富达投资(Fidelity)的彼得•林奇(Peter Lynch)常说,如果你每年花上15分钟研究经济,其中10分钟都是浪费的。理财顾问瑞克•埃德尔曼(Ric Edelman)为撰写《平凡人,非凡财富》(Ordinary People, Extraordinary Wealth)调研了5000位百万富翁,发现百万富翁每天平均只花六分钟在个人理财上。他们有更好的事情要做。

2. 创新思维

乔治•斯坦利(George Stanley)在其《百万富翁的智慧》一书中写道,“他们与常人的想法不同,收获也就不同。”是的,创新的想法可以致富。哪里有适合你独特天赋的独特机会,就到哪里去。《百万富翁的智慧》的中心思想就是:不要勉强适应,走你自己的路。

3. 始终积极向上

很多人都读过拿破仑•希尔(Napoleon Hill)的经典之作──《积极心态带来成功》(Success Through a Positive Mental Attitude)。一个有着26年军龄的特种部队教官在《快速公司》(Fast Company)杂志上的一段比喻非常贴切,他说,“如果有两个士兵,其中一个受过世界上所有的生存训练但心态消极,另一个只受过很少训练但心态积极,我担保一定是那个心态积极的士兵成功走出危险的森林。”就是这么回事。作为一名海军陆战队老兵,我知道他是对的。

4. 别做自己讨厌的事

许多人苟活在平静的绝望中,从事着他们厌恶的工作,等待退休。管理大师马库斯•白金汉(Marcus Buckingham)在其畅销书《你需要知道的一件事》(The One Thing You Need to Know)中直言不讳地指出,“确定什么事情是你不喜欢做的,然后停掉它。”

5. 做自己喜欢做的事

鼓舞人的话我们已经听了不少:跟随你的天赐之福;跟着兴趣走,钱自然会来。总之,最重要的是,永远不要忘记斯坦利所指出的:如果你有足够的创造力,能够选择一个理想的职业,你就能够取得极大的成功。杰出的百万富翁是那些选择了他们喜欢的事业的人。

6. 找到“真实的自己”

从事不适合自己的工作会令人疲劳、紧张,效率低下,表现不佳。你需要找到真实的自己并与之保持一致。如果需要,你可以向职业顾问咨询一下,或者读一读关于人格类型的书籍。在《百万富翁密码》中,我指出16种基本的人格类型,可以帮助未来的百万富翁们坚守梦想。白金汉的《发现你的优势》(Now Discover Your Strengths)也可借鉴。找到真实的自我,然后努力去实现自我,永远不要回头。

7. 投资“自己的公司”

替别人打工累了吗?你可以自己创业。读一读罗伯特•清崎(Kiyosaki)的著作《富爸爸,穷爸爸》(Rich Dad, Poor Dad),或者《 EBay傻瓜也能》(EBay for Dummies)。你可以开一家餐馆、干洗店,或者金属回收站。斯坦利的百万富翁名单中有许多人都是因为抓住了别人错过的机会。而且记住,大多数百万富翁都是为自己工作,积累自己的财富。

8. 富有激情

做一个有信仰的人,倾听内心深处的呼唤。不管它是爱情、亲情、爵士乐、艺术、高尔夫、写作、垂钓、发明还是慈善,都是你的天赐之福,无价之宝。我的精神导师约瑟夫•坎贝尔(Joseph Campbell)说过,“跟随天赐之福,你就会收获幸福,不管有没有钱。而追逐金钱,你可能会失去它,最后一无所有。”坎贝尔是《千面英雄》(The Hero of a Thousand Faces)一书的作者,他的着述还是乔治•卢卡斯(George Lucas)创作《星球大战》(Star Wars)的灵感来源,你看,他也具有百万富翁的心智。

9. 活在当下

沃伦•巴菲特(Warren Buffett)每天“跳着踢踏舞”去工作。他曾经告诉一群内布拉斯加大学(University of Nebraska)的学生,“我每天起床后都有机会做我最爱做的事,天天如此。如果你们想从我这儿学到什么,这是我能给你们的最佳建议。”接受这个建议吧。我们都要活在当下,尽情尽兴地过好每一天。

10. 改变世界

这也许是成为百万富翁的关键秘密,即使你现在还没什么钱:我们都有一些日常的压力,它要求我们在爱人、家庭和客户、老板之间,在我们今天的小世界和我们的未来之间找到平衡。百万富翁梦想使这个世界变得更好,他们心中有一个对所有人来说都更好的明天。他们喜欢帮助别人,在精神上和金钱上同时富有。我敢打赌你也有一个梦想,某种真正能使你的灵魂感到满足的东西。发现你生命的真正意义,超越你自己。你也能够成为一个百万富翁并改变世界。

记住,能否成为百万富翁完全取决于你的内心。只要端正态度,找准感觉,相信自己,你就是一个百万富翁,你就已经很富有了。你已经有了百万富翁的心智,金钱将随之而来。真的,就这么简单。

Ref :
http://cn.wsj.com/gb/20120416/inv075902.asp?source=newsletter