Skip to main content

Posts

Renaming SQL Server Default Instance

hi all, found another intresting stuff to post scenario: u change windows computer name and suddenly the query that contains SQL Server link server or anything that currently connect to ur "changed computer" is not working try using this query in SSMS -– Get the current name of the SQL Server instance for later comparison. SELECT @@servername –- Remove server from the list of known remote and linked servers on the local instance of SQL Server. EXEC master.dbo.sp_dropserver ‘[SERVER NAME]‘ –- Define the name of the local instance of SQL Server. EXEC master.dbo.sp_addserver ‘[NEW SERVER NAME]‘, ‘local’ –- Get the new name of the SQL Server instance for comparison. SELECT @@servername after try, u must restart sql server services...then check if the query is working now see u

Get Child and Parent from hierarchy Table using Common Table Expression (CTE) - SQL 2005 and above

hi, just want to post something that might be useful DECLARE @a AS TABLE ( autoid INT  , name VARCHAR(20) , parentid INT ) INSERT INTO @a (autoid, name, parentid) SELECT 1, '1', 0 UNION ALL SELECT 2, '1.1', 1 UNION ALL SELECT 3, '1.2', 1 UNION ALL SELECT 4, '1.2.1', 3 UNION ALL SELECT 5, '2.1', 2 UNION ALL SELECT 6, '3.1', 3 UNION ALL SELECT 7, '3.2', 3 /* 1 1 0 2 1.1 1 3 1.2 1 4 1.2.1 3 5 2.1 2 6 3.1 3 7 3.2 3 */ /* Get Parent Records from child '1.2.1' return: 1.2 (parent from '1.2.1') 1 (parent from '1.2') */ ;WITH    employeeMaster       AS ( SELECT   p.autoid ,                     p.parentid ,                     p.[name]            FROM     @a p            WHERE ...

Find size of SQL Server tables and other objects with stored procedure

my colleague needs a query to seek for all tables in certain Database then use it to display informati on such as: Created Date, Rows in that tables, etc at first i look over sp_spaceused but it end up wrapping query which is hard because undocumented and it failed :( also considerations can be used for SQL 2000 so i google it and found this link , it works for SQL 2000 and above i tweak a bit to meet my requirement but credit goes to  Richard Ding here is the query that i used USE master; GO IF OBJECT_ID(N'dbo.sp_SOS', N'P') IS NOT NULL   DROP PROCEDURE dbo.sp_SOS; GO CREATE PROCEDURE dbo.sp_SOS   @DbName sysname = NULL,   @SchemaName sysname = NULL,   @ObjectName sysname = N'%',   @TopClause nvarchar(20) = NULL,   @ObjectType nvarchar(50) = NULL,   @ShowInternalTable nvarchar(3) = NULL,   @OrderBy nvarchar(100) = NULL,   @UpdateUsage bit = 0 AS /*=============================================================...

Print Escape Character for printer Barcode in .NET

fiuhh, this case is one of the most annoying 'simple' approach requirement is to send 'barcode' to printer directly, because often we using Image print (creating image on the fly then send it to barcode printer)..this one althought works but often the image become blur / discarded so we need to 'communicate' to barcode printer using its 'language' the 'language' depends on the printer itself..in my case, we using SATO barcode printer so the 'language' is SBPL  (visit the link for more info) first we need to design the label we can use SBPL and design it 'on the fly' make us a little bit crazy or we can use SATO LABEL Gallery Free to do the job and generate SBPL equivalent for us so we can use it in our code after we design it, we use "Print to file" method for the printer then print it so we can see what its send to the printer the generated SBPL will contain 'unseen' character in visual studio ID...

Reset Identity Column Value in SQL Server

if u have identity column and of course before deployed it to production, u'll need to test the value when u done, u probably want to reset it to initial value (0 for example) so u have 'fresh' data for production u'll be need to use this syntax DBCC CHECKIDENT (orders, RESEED, 0) PS: the next value will be always +1, so in my case next value will be 1 until then