15 Ekim 2019 Salı

Temporary Stored Procedures in Sql Server

Temporary stored procedures are similar to temporary tables. It creates in the tempdb. There are 2 kinds of temp procs: temporary stored procedure and global temporary stored procedure. Temporary stored procedure is only accessible in the session that created it. But we can access global temporary stored procedures from other sessions.

How to Create Temporary Stored Procedure

You can execute temporary stored proc like any other ordinary stored proc. But we must use # symbol before stored proc name.

Example:

create proc #tmpProc
(@dbid int)
as
begin
         select loginame,
DB_NAME(dbid) as dbName 
from sys.sysprocesses where dbid=@dbid
       
end

You must execute in the same session.

exec #tmpProc 5





How to Create Global Temporary Stored Procedure

We must use ## symbol before stored proc name.

create proc ##tmpProcGlobal
(@dbid int)
as
begin
         select loginame,
DB_NAME(dbid) as dbName 
from sys.sysprocesses where dbid=@dbid
       

end

You can execute in the other sessions. But if the main session (created session) closes you cannot access the global temporary stored procedure.

exec ##tmpProcGlobal 5


Why do we use temporary stored procs?

Stored Procedure slowness is a frequent problem for developers. Generally, slow stored procedure is renamed to produce a new stored procedure. And then it is tested. However, the procedure under test is often forgotten. This can cause problems and may return the database to the trash. Database administrator does not like it. To prevent this, a temporary stored procedure can be used. Because when the session closes, the temporary procedure is deleted.





13 Ekim 2019 Pazar

How to get ram and cpu information from within SQL Server?

xp_instance_regread -> It is an undocumented stored proc for reading registry settings. 


If the following script is run, it returns cpu mark, model information.


EXEC xp_instance_regread
'HKEY_LOCAL_MACHINE',
'HARDWARE\DESCRIPTION\System\CentralProcessor\0',
'ProcessorNameString';


sys.dm_os_sys_info -> The other method to collect cpu information is sys.dm_os_sys_info system view. It contains logical cpu count,physical cpu count, total memory etc..


I prepared a script that how to get cpu some cpu information within Sql Server.


--- How to get cpu-ram  


drop table if exists #tmp1 ---may run on sqlserver 2016 and later

create table #tmp1(id int default(1),Val varchar(100),
[Data] varchar(100))


insert into #tmp1(Val,[Data])
EXEC xp_instance_regread
'HKEY_LOCAL_MACHINE',
'HARDWARE\DESCRIPTION\System\CentralProcessor\0',
'ProcessorNameString';


select compname as ComputerName, 
       Data    as ProcessorName,
  Number_of_Logical_CPU,
  hyperthread_ratio,
  Number_of_Physical_CPU,
  Total_Physical_Memory_IN_KB
from (
select 1 as id,
SERVERPROPERTY('MachineName')  as compname ) as tbl
inner join #tmp1 on tbl.id=#tmp1.id
inner join (
SELECT 1 as id,
cpu_count AS [Number_of_Logical_CPU]
,hyperthread_ratio
,cpu_count/hyperthread_ratio AS [Number_of_Physical_CPU]
,physical_memory_kb/1024 AS [Total_Physical_Memory_IN_KB]
FROM sys.dm_os_sys_info
) as tbl2 on tbl.id=tbl2.id OPTION (RECOMPILE);




9 Ekim 2019 Çarşamba

How to listen SqlServer on multiple ports?





We can connect SQL Server on multiple ports. How can we do this step-by-step? 1- Open SQL Server Configuration Manager. 2- Click to expand SQL Server Network Configuration 3- Select your instance 4- On the right side, right click on TCP/IP then select properties.
























5- In the tcp/ip window, select ip addresses tab 6- At the bottom of the window there is a IPAll part
































7- In the TCP Port field, enter each port number separated by commas. Then click OK. 8- A pop up message will be appear. Click ok.




You must restart Sql Server Service. Stop and start Sql Server Service or restart. After the restarting, SQL Server will be listening on all the port numbers you specified. 9-If you would like to verify changes you can look at the Sql Server Logs. If it is successful you should see below messages in Sql Server Logs: Server is listening on [ ‘any’ <ipv4> 5555]. Server is listening on [ ‘any’ <ipv6> 5555]. Server is listening on [ ‘any’ <ipv4> 6666]. Server is listening on [ ‘any’ <ipv6> 6666]. Server is listening on [ ‘any’ <ipv4> 7777]. Server is listening on [ ‘any’ <ipv6> 7777]. 10-The other way to control connection is creating a UDL file. Create a udl file. Check the connection by entering the IP number and port. 





                                                      














7 Ekim 2019 Pazartesi

How to check current identity value?

There are several ways of getting current IDENTITY values.
Let's create a table and examine on a sample. 


use AdventureWorks2017
go
create table News(
id   int identity(1,1),
name  varchar(20)
);
go

---- insert some records
insert into News values('BBC'),('CNN'),('TRT')


1- First option @@identity

select @@IDENTITY



As you can see it returns 3. But if you open a new query tab and execute select @@IDENTITY again. It returns NULL because @@identity returns the last generated identity value on the opened connection.

2-The other option is DBCC command.

dbcc checkident('dbo.News',noreseed)    

Result Messages are:

Checking identity information: current identity value '3', current column value '3'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

3- The third option is ident_current function.

select IDENT_CURRENT('dbo.News')


The advantage of this version is that you can use in select query. In this option we should use information_schema.tables to list all tables' identities. 

For example:

select table_schema as schName,        
       table_name   as tableName,     
       ident_current(table_schema + '.' + table_name) as identity_Val
from information_schema.tables
where objectproperty(object_id(table_schema + '.' + table_name), 'TableHasIdentity') = 1


4- You can use sys.identity_columns system view to list all current identities.

SELECT       OBJECT_NAME(OBJECT_ID) AS TABLENAME, 
             NAME AS COLUMNNAME, 
             SEED_VALUE, 
             INCREMENT_VALUE, 
             LAST_VALUE 
FROM     sys.identity_columns 
where OBJECT_ID in (select object_id from sys.objects where type='U')


















2 Ekim 2019 Çarşamba

What setup files do we need to learn Sql Server Management?

What setup files do we need to install Sql Server?
Where can we download them?


VIRTUAL BOX: Maybe you want to setup virtualbox before setup OS. 
Maybe you would like to setup OS on virtualbox.


WINDOWS 2016 OS: You can use this version 180 days free.


SQL 2016:You can use this version 180 days free.


SQl Server 2017 Developer:It is free product.


SQL SSMS:It is free product not evaluation version.


SQL SSDT: You can download SSDT with Visual Studio 2017 or you can download 
SSDT standalone installer.  SSDT tools contains Analysis Services, 
Integration Services, Reporting Services.  


Temporary Stored Procedures in Sql Server

Temporary stored procedures are similar to temporary tables. It creates in the tempdb. There are 2 kinds of temp procs: temporary stored pro...