When a TSQL script creates a temporary table, it is useful in developing mode to drop the table if it exists.
The following script does the job:
IF OBJECT_ID( N'tempdb..#MyTempTable') IS NOT NULL
DROP TABLE #MyTempTable
When a TSQL script creates a temporary table, it is useful in developing mode to drop the table if it exists.
The following script does the job:
IF OBJECT_ID( N'tempdb..#MyTempTable') IS NOT NULL
DROP TABLE #MyTempTable
The task of restoring a SQL Server database with a TSQL script is as simple as executing the following script:
RESTORE DATABASE [MyDatabase] FROM DISK = N'C:\Backup\MyDatabase.bak' WITH FILE = 1, MOVE N'MyDataLogicalName' TO N'D:\DatabasesData\MyDatabase.mdf', MOVE N'MyLogLogicalName' TO N'D:\DatabasesLog\MyDatabase_log.ldf',
The only problem with the previous script is that if there are any open connections to the database the following error is generated:
Exclusive access could not be obtained because the database is in use.
To fix the issue one solution is to put the database in single user mode before the backup.
-- Put the database in single user Mode ALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
-- Restore the Database RESTORE DATABASE MyDatabase FROM DISK ...
-- Put the database back in multiuser mode ALTER DATABASE MyDatabase SET MULTI_USER GOIf this script fails, execute the last statement to put the data base back in multiuser mode.
To render a aspx page to a HTML simply execute the following code:
1: StringWriter writer = new StringWriter();
2: HttpContext.Current.Server.Execute("~/MyPage.aspx", writer);
To convert to a string:
1: writer.ToString()SQL Server returns the error
The database principal owns a schema in the database, and cannot be dropped
when a user is being dropped that owns a schema in the database.
The delete the user go to Sql Server Management Studio, expand your database -> Security and press on Schemas. In the Object Explorer Details (if not visible go to the View Menu e select Object Explorer Details) you can see a list of the schemas and the owners.
Now locate the schema(s) the user you want to delete is the owner, right click and select properties. In the General you can see the schema owner, change it to the new owner (dbo for example).
When the user you want to delete has no schemas owned you can delete it.
Concatenation of strings with a null value will result in NULL.
For example:
DECLARE @NullString VARCHAR
SELECT @NullString = NULL
SELECT 'MyString' + @NullString
Will return NULL and not ‘MyString.
Until now we could use
SET CONCAT_NULL_YIELDS_NULL OFF
When concatenating a null value with a string yields the string itself (the null value is treated as an empty string).
Microsoft has issued the following warning:
In a future version of SQL Server CONCAT_NULL_YIELDS_NULL will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
And this leaves us with the ISNULL solution:
DECLARE @NullString VARCHAR
SELECT @NullString = NULL
SELECT 'MyString' + ISNULL(@NullString, '')
After retrieving an object with Linq to Sql, if an update (Attach) is tried on the same object, the following exception is thrown:
An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.
Example:
Product product = null;Product originalProduct = new Product();// Gte product to change its propertiesusing (myDataContext dc1 = new myDataContext())
{product = dc1.Products.Single(p=>p.ID==457);
}
product.Description = "MyProduct";using (myDataContext dc2 = new myDataContext())
{dc2.Products.Attach(product , originalProduct);
dc2.SubmitChanges();
}
The best solution to fix this behavior is to set the DeferredLoadingEnabled property of the DataContext to false of the retrieve entity query.
Example:
Product product = null;Product originalProduct = new Product();// Gte product to change its propertiesusing (myDataContext dc1 = new myDataContext())
{ dc1.Datacontext.DeferredLoadingEnabled = false;product = dc1.Products.Single(p=>p.ID==457);
}
product.Description = "MyProduct";using (myDataContext dc2 = new myDataContext())
{dc2.Products.Attach(product , originalProduct);
dc2.SubmitChanges();
}
The DeferredLoadingEnabled property of the DataContext controls the deferred loading options of LINQ.
A SSIS package can be executed using SQL Server agent.
To execute the package a Proxy Account and a credential must be configured in SQL Server.
Problems:
1) Executed as user: DOMAIN\user. The process could not be created for step 1 of job 0xB013D0354C8CBD46B79E948740EF5441 (reason: 1314). The step failed.
The error 1314 is "A required privilege is not held by the client".
This message indicates that the SQL Server Service account doesn't have the required rights to switch the security context to the proxy account.
To fix it verify:
1) The proxy account have the "Act as part of the operating system" privilege.
2) The SQL Server Account is in the group
SQLServer2005MSSQLUser$<server>$<instance>
3) The Proxy account is in the group
SQLServer2005DTSLUser$<server>$<instance>
Sending a mail form an integration service package (SSIS) is implemented using the .Net framework classes.
The only question is how to use a SMTP Connection Manager task to configure the SMTP Server.
Public Sub Main()
Dim mailMessage As MailMessage
Dim smtpClient As SmtpClient
Dim smtpConnectionString As String = DirectCast(Dts.Connections("SMTP Connection Manager").AcquireConnection(Dts.Transaction), String) Dim smtpServer As String = smtpConnectionString.Split(New Char() {"="c, ";"c})(1)mailMessage = New MailMessage("from@myMail.net", "to@myMail.net")
mailMessage.Body = "myMessage" mailMessage.Subject = "mySubject"smtpClient = New SmtpClient(smtpServer)
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials
mailMessage.IsBodyHtml = True
smtpClient.Send(mailMessage)
Dts.TaskResult = Dts.Results.Success
End Sub
The Max Degree of Parallelism or MAXDOP is a configuration indicating how the SQL Server optimizer will use the CPUs.
The value must be equal to the number of physical processors, not including hyperthreading.
One possible calculation formula is:
select case
when cpu_count / hyperthread_ratio > 8 then 8
else cpu_count / hyperthread_ratioend as optimal_maxdop_setting
from sys.dm_os_sys_info;
Configure via Management Studio:
1. Open SQL Server 2005 Management Studio
2- Once the tool loads, navigate to the intended server in the Object Explorer and right click on server name
3. Select the 'Properties' option
4. Select the 'Advanced' page from the left navigation
5. Review the configurations for the 'Parallelism' section
Configure via T-SQL Script:
sp_configure 'show advanced options', 1;GORECONFIGURE WITH OVERRIDE;
GOsp_configure 'max degree of parallelism', 8;GORECONFIGURE WITH OVERRIDE;
GO1. T-SQL
select * from Orders o
where exists (select 1 from Product where ProductType = 'Memory'
and ProductId = o.ProductID)2. Linq
using (MyDataContext dc = new MyDataContext())
{ var orders = from o in dc.Orders where (from p in dc.Products
where p.ProductType == 'Memory'
select p.ProductID).Contains(o.ProductID)
select o;
}