Monday, March 28, 2011
Custom Reports using Crystal Reports and MySQL
Tuesday, March 22, 2011
DBCC Shrinkfile unable to Shrinkfile
Many times it happens that you will not able to shrink datafile using DBCC Shrinkfile command. There may be various reason/error for it. One of them says as mentioned below:
"file id of database id cannot be shrunk as it is either being shrunk by another process or is empty"
This happens when already a DBCC shrinkfile has been run and due to some reason it was unable to shrink the file and the internal files are not re-organized or file has not grown to shrink. Or SQL server somewhere it feels that the file you are shrinking is in middle of Shrinking process
One of the solution for this file is to add some MB of data to the file which you are trying to shrink and then use DBCC Shrinkfile. This will work as adding a few MB to the data file resets an internal counter or switch that tells it it's not in the middle of a shrink now
Wednesday, March 16, 2011
Impersonating Sql User
As a SQL server DBA many times you may get to hear from users that he is not able to access the database, SQL server or any of the objects in SQL server. This may sometimes that the user is passing a wrong credentials or may be is trying to connect to SQL server through some applications but due to some ODBC / OLEDB connectivity problem the application is not able to connect even though user is passing correct credentials. Sometimes the issue may be genuine too that he is actually not having the access. So as a DBA what we can take up as a initial troubleshooting is to Impersonating the SQL user account to check whether is user having access to database or not. So below mentioned are the steps to be carried out.
Step1. Connect to SQL Server with Sysadmin Privilege account ( Normally DBA’s will be having SysAdmin Privilege)
Step2. Select a New Query from Menu and go to Database the user is trying to access using below command.
USE [ProblemDatbaseName]
GO
Step3. Now Impersonate the account using below SQL query and check for the status.
EXECUTE AS USER = 'YOURDOMAIN\ACCOUNTNAME'
If the user account is invalid you will get the below Error:
Msg 15404, Level 16, State 11, Line 1
Could not obtain information about Windows NT group/user 'YOURDOMAIN\ACCOUNTNAME', error code 0x534.
Else if you get the output as “command (s) completed successfully” then check for the access using below command.
SELECT SUSER_NAME(),USER_NAME()
Now the above query will give you username and access.
If you want to check for specific permission you can do so by using below command. If the result set returns 1 means he is a member of that permission for example I have taken db_owner in below example. If not it will return you 0.
SELECT IS_MEMBER('db_owner')
Step 4. Now run REVERT to go back to execution from your credentials.
The above process can generally be used when testing a database user’s permission. Hope this is of some help. Thank you.
Tuesday, March 15, 2011
Cannot use the PAGE granularity hint on the table
This seems to be an very interesting error which one may receive while running DBCC ShrinkDatabase command.
The full error is as follows
Cannot use the PAGE granularity hint on the table “tablename” because locking at the specified granularity is inhibited.
The error occurs when you have one or more non-clustered indexes created on table without a clustered index on table. One should always create a clustered index on table, without which it does not make more sense.
The resolution for above error is just to create a Clustered Index on the table on which you are getting error. Will try to dig in and get more info so as to what exactly causes this error and will update you all.
Hope this information is helpful.
Friday, March 11, 2011
Dropping A Role in SQL server using T-SQL
It happens so that when you drop/delete a Database Role in SQL Server Database, the Role members who were assigned to this role wont get de-associated from this Role. No doubt the role will be dropped/deleted but the member will be having this Role still existing in their permission list. So I came up with the below T-SQL which will first de-associate all the role member’s from the role to be dropped and then it will go ahead and drop the role. For Testing Purpose, I have taken Role “db_execute” as the role which needs to be dropped/deleted.
/* De-Associating RoleMember From Role To Be Dropped*/
USE [AdventureWorks]
GO
DECLARE @RoleName sysname
set @RoleName = N'db_execute'
IF EXISTS (SELECT * FROM dbo.sysusers WHERE name = @RoleName AND issqlrole = 1)
Begin
DECLARE @RoleMemberName sysname
/* Cursor to Loop in for Each Member have the Role Privilege and Drop RoleMember */
DECLARE Member_Cursor CURSOR FOR
select [name]
from dbo.sysusers
where uid in (
select memberuid
from dbo.sysmembers
where groupuid in (
select uid
FROM dbo.sysusers where [name] = @RoleName AND issqlrole = 1))
OPEN Member_Cursor;
FETCH NEXT FROM Member_Cursor
into @RoleMemberName
WHILE @@FETCH_STATUS = 0
BEGIN
exec sp_droprolemember @rolename=@RoleName, @membername= @RoleMemberName
FETCH NEXT FROM Member_Cursor
into @RoleMemberName
END;
CLOSE Member_Cursor;
DEALLOCATE Member_Cursor;
/* End Of Cursor */
end
GO
/* Checking If Role Name Exists In Database */
IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'db_execute' AND type = 'R')
DROP ROLE [db_execute]
GO
SELECT @@SERVERNAME Showing Null
Instance | Server information |
Default instance | 'servername' |
Named instance | 'servername\instancename' |
failover clustered instance - default instance | 'virtualservername' |
failover clustered instance - named instance | 'virtualservername\instancename' |
Tuesday, March 8, 2011
Logon failure: unknown user name or bad password
Exact error goes as follows:
Unable to start execution of step (reason: Error authenticating proxy , system error: Logon failure: unknown user name or bad password.). The step failed.
This happens when the secret word(password) you provided while creating a credential for a proxy has got expired. Exact scenario may be, you have used a domain account which enforces password expiration policy and you have changed you password. This happens because sql server resolves your changed password when you authentication mode is set to accept mixed mode, but the credentials which you have created does not accept this or resolve the change. Now you if you have to make this proxy work you need to delete the credential and again create it with new password and map to proxy. This sometime may again hit your jobs. So better way is to alter the credential and provide the new password. This can easily be achieved by below script by replacing the values according as per your needs:
Script:
USE [master]
GO
ALTER CREDENTIAL [ProxyCredentialsName] WITH IDENTITY = N'DOMAIN\ACCOUNTNAME', SECRET = N'NewPasswordchanged'
GO