Pages

Showing posts with label SQL Azure. Show all posts
Showing posts with label SQL Azure. Show all posts

Wednesday, January 28, 2015

Change SQL Server user login account password

Change SQL Server user login account password script

in following Example :
Login account user name is : dbdevwebuser
Current password is : MyCurrentPwd
New password is : MyNewCurrentPwd

We can use following script to change the users login password.
ALTER LOGIN dbdevwebuser WITH PASSWORD = N'MyNewCurrentPwd' OLD_PASSWORD = 'MyCurrentPwd'

Monday, January 12, 2015

Find Most Expensive Queries Using DMV


Find Most Expensive Queries Using DMV


Following is the query which finds out top 10 most expensive queries

SELECT TOP 10 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(qt.TEXT)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2)+1),
qs.execution_count,
qs.total_logical_reads, qs.last_logical_reads,
qs.total_logical_writes, qs.last_logical_writes,
qs.total_worker_time,
qs.last_worker_time,
qs.total_elapsed_time/1000000 total_elapsed_time_in_S,
qs.last_elapsed_time/1000000 last_elapsed_time_in_S,
qs.last_execution_time,
qp.query_plan
FROM sys.dm_exec_query_stats  qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle)  qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY qs.total_logical_reads DESC -- logical reads
-- ORDER BY qs.total_logical_writes DESC -- logical writes
-- ORDER BY qs.total_worker_time DESC -- CPU time

Source : http://blog.sqlauthority.com/2010/05/14/sql-server-find-most-expensive-queries-using-dmv/

Monday, December 29, 2014

Sql Azure : Way Around for Sp_depends in Sql Azure

You can use following query to find out table used in Stored procedures similar to sp_depends.

SELECT ReferencingObjectType = o1.type,
       ReferencingObject = SCHEMA_NAME(o1.schema_id)+'.'+o1.name,
       ReferencedObject = SCHEMA_NAME(o2.schema_id)+'.'+ed.referenced_entity_name,
       ReferencedObjectType = o2.type
FROM   sys.sql_expression_dependencies ed
       INNER JOIN  sys.objects o1
         ON ed.referencing_id = o1.object_id
       INNER JOIN sys.objects o2
         ON ed.referenced_id = o2.object_id
WHERE o1.type in ('P','TR','V', 'TF') and ed.referenced_entity_name like '<Table Name>'
ORDER BY ReferencingObjectType, ReferencingObject

replace <Table name>  with your actual table name which you want to search

Steps to Create a customize Role for Azure database

Steps to Create a customize Role for Azure database
Download "Azure User Management Console" tool from https://aumc.codeplex.com/

Create a Login
Use “Azure User Management Console”
EG: dbdevwebuser
Create User for login on master
Connect to master database, and execute the following:
CREATE USER dbdevwebuser FOR LOGIN dbdevwebuser WITH DEFAULT_SCHEMA=[dbo]

This has created user “dbdevwebuser” for the login “dbdevwebuser”.
Note: You can keep the login and the user name separate.

Create User for login on the database to be used by application
Connect to application database, and execute the following:
CREATE USER dbdevwebuser FOR LOGIN dbdevwebuser WITH DEFAULT_SCHEMA=[dbo]

Create Customized Role 
Connect to application database, and execute the following:
CREATE ROLE [db_executor] AUTHORIZATION [dbo]
This will create a role by the name “db_executor”

Grant Permissions to Customized Role 
Here I am trying to create a role to grant execute for all stored procedure
GRANT EXECUTE TO [db_executor]

This will grant execute permission to the role “db_executor”
Note: You can “GRANT” more permission to this role, to make it more customized.

Assign Roles to User Created
Assign different roles of your choice to the user
sp_addrolemember @rolename = 'db_executor', @membername = 'dbdevwebuser'
sp_addrolemember @rolename = 'db_datareader', @membername = 'dbdevwebuser'
sp_addrolemember @rolename = 'db_datawriter', @membername = 'dbdevwebuser'

This will assign roles of “db_executor”, “db_datawrited” and “db_datareader” to the user 'dbdevwebuser' on the application database.
NOTE:
1. These kind of roles are usually used when you want your application to execute procedures, read and write data. But do not want them to manipulate any Schema.

Testing working of the User for the Roles Assigned

This user should
1. Execute procedure
2. Select, Insert, Update, Delete from the database
3. Create Temporary tables
4. Select View
This user should not:
1. Create, ALTER, Delete Table
2. Create, ALTER, Delete Procedure
3. Create, ALTER, Delete View
4. Create, ALTER, Delete Function
5. See Content “sp_helptext” for a Procedure


How to make a database copy from production to staging server in Azure

On the same server




1.       Connect to destination server (Here Staging DB) “master” database with productions username and pwd  because we need to have same username with pwd on source and destination DB.

2.       Execute the following command:
a.       CREATE DATABASE [NEWDATABASENAME] AS COPY OF [SOURCE_SERVERNAME].[SOURCEDATASE]
b.      IMP: Replace the NEWDATABASENAME, SOURCE_SERVERNAME and SOURCEDATASE with correct names
To track progress you can run following command or you can connect to Azure portal and check the progress in database tab

3.       Execute: Select *  from sys.dm_database_copies, this will give you the stats of the % of database copied

How to rename a SQL Azure database?

1. Connect to your Database
2. Select Master Database
3. Type following Query

Syntax:
alter database <old-database-name> modify name = <new-database-name>

Description:
<old-database-name> is your old DB name
<new-database-name> is your new DB name
Note: Don't use single or double quotes around DB Name

Example:
alter database MyStudent modify name = StudentMaster


Your DB name is changed :)