Query the registry on XP with REG QUERY  

Posted by ReelTym

What is Reg.exe?
Reg.exe is a command-line utility that can be used to edit the Windows registry. Using REG you can perform almost all tasks that can be done using the window-based Regedit.exe tool.
To run the REG command, open the Command Prompt window, type REG along with its argument, and press Enter. You can use the REG command to perform a number of registry related tasks. In this article, we will see how to query, add, and remove registry values.


Query Registry Keys
Using REG QUERY, you can:
  • Query a single key for a single value.
  • Query a range of registry keys for all their values.
Querying enables you to quickly check the value or values of a particular key.
Syntax
REG QUERY Key_Name [/v Value_Name | /ve] [/s]
  • Key_Name must provide the full path to the key you need to query, represented by \\Computer_Name\Full_Key_Name. The Full_Key_Name comprises Root_Key\Sub_Key. Root_Key name may be HKLM, HKCU, HKCR, HKU, or HKCC. Sub_Key is the full name of the queried key under the Root_Key. Computer_Name is used when you are accessing a remote registry key to specify the name of the computer you are accessing.
  • /v enables you to query the specified registry key.
  • Value_Name is the name of the queried value under the registry key.
  • /ve helps you query the empty value name <no name> or the default value of the registry key.
  • /s enables you to query all subkeys and values under the specified registry key.
Examples
reg query hklm\software\McAfee
HKEY_LOCAL_MACHINE\software\McAfee
HKEY_LOCAL_MACHINE\software\McAfee\VirusScan

Add New Registry Keys
Using REG ADD, you can:
  • Add value to an existing registry key.
  • Add a new registry key without any values.
  • Create a new registry key and assign a value to it.
Syntax
REG ADD Key_Name [/v Value_Name | /ve] [/t Type] [/s Separator] [/d Data] [/f]
  • Key_Name provides full path to the selected key and is represented by \\Computer_Name\Full_Key_Name. On remote computers, you can add keys under the HKLM and HKU root keys only.
  • /v is used to specify the Value_Name that you want to add under the selected key.
  • /ve enables you to add an empty value name <no name> value for the specified registry key.
  • /t is used to specify the data type of the new registry key–REG_SZ, REG_MULTI_SZ, REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_DWORD_LITTLE_ENDIAN, REG_BINARY,REG_NONE, REG_EXPAND_SZ. The data type REG_SZ is assumed by default if you do not specify the data type in the command.
  • /s is used for REG_MULTI_SZ data types to specify the one character that you use as the separator in your data string. If not specified, “” is used as separator by default.
  • /d enables you to assign data to the Value_Name you are adding.
    /f enables you to forcefully overwrite the current registry entry without displaying the confirmation message.
Examples
reg add \HKLM\Software\MySoftware
The command adds the \HKLM\Software\MySoftware key in the registry .
reg add HKLM\Software\MySoftware /v Path /t REG_EXPAND_SZ /d %%systemroot%%
The command adds a value with the name Path, type REG_EXPAND, and data %systemroot% under the registry key HKLM\Software\MySoftware.

Remove a Registry Key
When you delete a registry key using the REG tool, all subkeys and values under this key are removed from the registry. However, the system will prompt you to confirm your decision before actually deleting any key. Using REG you can delete only those keys to which you are allowed to access.
Syntax
REG DELETE Key_Name [/v Value_Name | /ve | /va] [/f]
  • Key_Name is represented by \\Computer_Name\Full_Key_Name. On remote computers, you can add keys under the HKLM and HKU root keys only.
  • Value_Name is the name of value under the selected key that you want to delete. If you do not include the Value_Name in the command all subkeys and values under the selected key are deleted.
  • /ve option enables you to delete the values of empty value name.
  • /va option enables you to delete all values under the specified key.
  • /f is used to delete the specified key without confirming your decision.
Example
reg delete HKLM\Software\ABCsoftware
The command deletes all subkeys and values located under the ABCsoftware registry key.
REG provides you with an easy and quick way to perform various registry-related tasks. The main utilization of the REG command is to embed registry operations in batch files and logon scripts. Some of the command registry operations that you can perform using REG are, query, add, and remove registry keys. You may also use it to perform tasks such as update, copy, save, load, unload, backup, and restore registry keys.

ROLLBACK after TRUNCATE TABLE in a Transaction  

Posted by ReelTym

if object_id( 'Testing123' ) is not null
    drop table dbo.Testing123;
go

create table Testing123 ( ordinal int identity(1,1) primary key clustered, data varchar(max) )
go

insert into Testing123 ( data )
    select o1.name + ' - ' + o2.name
        from sys.objects o1
        cross join sys.objects o2
go

--Now we'll look at the count of rows, and the pages that belong to the table.

select count(*) from dbo.Testing123; 
go 
dbcc ind('Test', 'Testing123', -1); 
go

--We get a count of 504 rows and the following pages:

/*
(3025 row(s) affected)

-----------
3025

(1 row(s) affected)

PageFID PagePID     IAMFID IAMPID      ObjectID    IndexID     PartitionNumber PartitionID          iam_chain_type       PageType IndexLevel NextPageFID NextPagePID PrevPageFID PrevPagePID
------- ----------- ------ ----------- ----------- ----------- --------------- -------------------- -------------------- -------- ---------- ----------- ----------- ----------- -----------
1       154         NULL   NULL        21575115    1           1               72057594038845440    In-row data          10       NULL       0           0           0           0
1       153         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           156         0           0
1       155         1      154         21575115    1           1               72057594038845440    In-row data          2        1          0           0           0           0
1       156         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           157         1           153
1       157         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           158         1           156
1       158         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           159         1           157
1       159         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           168         1           158
1       168         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           169         1           159
1       169         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           176         1           168
1       176         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           177         1           169
1       177         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           178         1           176
1       178         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           179         1           177
1       179         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           180         1           178
1       180         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           181         1           179
1       181         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           182         1           180
1       182         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           183         1           181
1       183         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           184         1           182
1       184         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           185         1           183
1       185         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           186         1           184
1       186         1      154         21575115    1           1               72057594038845440    In-row data          1        0          0           0           1           185

(20 row(s) affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.
*/

--Next, start a transaction and truncate the table. Verify that DBCC IND shows no pages belonging to the table and the count is 0. Then, look at the locks.

begin tran 
    truncate table dbo.Testing123; 

    insert into Testing123 ( data )
        select o1.name
            from sys.objects o1

    select count(*) from dbo.Testing123;

    dbcc ind('Test', 'Testing123', -1); 
    dbcc extentinfo('Test', 'Testing123', -1);

    select resource_type, resource_description, request_mode
        from sys.dm_tran_locks 
        where resource_type in ('EXTENT', 'PAGE') 
        and resource_database_id = db_id('Test');

--You should see 2 rows from DBCC IND, and 55 rows from count(*). But the locks info should return something like:

/*

(55 row(s) affected)

-----------
55

(1 row(s) affected)

PageFID PagePID     IAMFID IAMPID      ObjectID    IndexID     PartitionNumber PartitionID          iam_chain_type       PageType IndexLevel NextPageFID NextPagePID PrevPageFID PrevPagePID
------- ----------- ------ ----------- ----------- ----------- --------------- -------------------- -------------------- -------- ---------- ----------- ----------- ----------- -----------
1       155         NULL   NULL        149575571   1           1               72057594038976512    In-row data          10       NULL       0           0           0           0
1       154         1      155         149575571   1           1               72057594038976512    In-row data          1        0          0           0           0           0

(2 row(s) affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.
file_id     page_id     pg_alloc    ext_size    object_id   index_id    partition_number partition_id         iam_chain_type       pfs_bytes
----------- ----------- ----------- ----------- ----------- ----------- ---------------- -------------------- -------------------- ------------------
1           154         1           1           149575571   1           1                72057594038976512    In-row data          0x6000000000000000

(1 row(s) affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.
resource_type                                                resource_description                                                                                                                                                                                                                                             request_mode
------------------------------------------------------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------
PAGE                                                         1:155                                                                                                                                                                                                                                                            X
PAGE                                                         1:153                                                                                                                                                                                                                                                            X
PAGE                                                         1:182                                                                                                                                                                                                                                                            X
PAGE                                                         1:183                                                                                                                                                                                                                                                            X
PAGE                                                         1:180                                                                                                                                                                                                                                                            X
PAGE                                                         1:181                                                                                                                                                                                                                                                            X
PAGE                                                         1:178                                                                                                                                                                                                                                                            X
PAGE                                                         1:179                                                                                                                                                                                                                                                            X
PAGE                                                         1:176                                                                                                                                                                                                                                                            X
PAGE                                                         1:177                                                                                                                                                                                                                                                            X
EXTENT                                                       1:176                                                                                                                                                                                                                                                            X
EXTENT                                                       1:216                                                                                                                                                                                                                                                            X
EXTENT                                                       1:208                                                                                                                                                                                                                                                            X

(13 row(s) affected)

*/

--So the extent and page locks include all the pages that we saw in the DBCC IND output. (Remember, an extent is 8 contiguous pages, starting at the one that describes the extent.)

--Only after you ROLLBACK the transaction will the locks be released, and you should see all the rows and pages back in the table again.

    rollback tran; 
go 
select count(*) from dbo.Testing123; 
dbcc ind('Test', 'Testing123', -1); 
go

/*

-----------
3025

(1 row(s) affected)

PageFID PagePID     IAMFID IAMPID      ObjectID    IndexID     PartitionNumber PartitionID          iam_chain_type       PageType IndexLevel NextPageFID NextPagePID PrevPageFID PrevPagePID
------- ----------- ------ ----------- ----------- ----------- --------------- -------------------- -------------------- -------- ---------- ----------- ----------- ----------- -----------
1       154         NULL   NULL        21575115    1           1               72057594038845440    In-row data          10       NULL       0           0           0           0
1       153         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           156         0           0
1       155         1      154         21575115    1           1               72057594038845440    In-row data          2        1          0           0           0           0
1       156         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           157         1           153
1       157         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           158         1           156
1       158         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           159         1           157
1       159         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           168         1           158
1       168         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           169         1           159
1       169         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           176         1           168
1       176         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           177         1           169
1       177         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           178         1           176
1       178         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           179         1           177
1       179         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           180         1           178
1       180         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           181         1           179
1       181         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           182         1           180
1       182         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           183         1           181
1       183         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           184         1           182
1       184         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           185         1           183
1       185         1      154         21575115    1           1               72057594038845440    In-row data          1        0          1           186         1           184
1       186         1      154         21575115    1           1               72057594038845440    In-row data          1        0          0           0           1           185

(20 row(s) affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.
*/

The Boy Scout Handbook  

Posted by ReelTym

A GUIDE TO ADVENTURE, A GUIDEBOOK FOR LIFE

SQL Server System Views Map (2008/2005)  

Posted by ReelTym

The Microsoft SQL Server System Views Map shows the key system views included in SQL Server, and the relationships between them.


SQL 2008
SQL 2005