sql-server-2012

sp_send_dbmail fails with attachment

痴心易碎 提交于 2019-12-04 11:34:37
I need to send email with file attachments. The attachment has to come from image type data field. When running the following code in both a query window and also stored procedure, no email is sent. in query window, it just says 'command(s) completed successfully', but no email. EXEC msdb.dbo.sp_send_dbmail @recipients = 'xx@xx.com', @subject = 'test', @execute_query_database = 'myDB', @body = 'test', @body_format = 'HTML', @profile_name = 'myProfile', @append_query_error = 1, @query = 'Select docData from [myDB].[dbo].[Documents] Where id = 1', @query_result_header = 0, @attach_query_result

Numbering islands in SQL Server 2012

耗尽温柔 提交于 2019-12-04 11:22:16
I need to number islands in SQL Server 2012. Island is defined as a set of rows where there is no day gaps between DateFrom and DateTo within the same ItemId ). The following dataset: CREATE TABLE #Ranges (ItemId INT, DateFrom DATETIME, DateTo DATETIME) INSERT INTO #Ranges VALUES (1,'2015-01-31','2015-02-17') INSERT INTO #Ranges VALUES (1,'2015-02-18','2015-03-31') INSERT INTO #Ranges VALUES (1,'2015-04-14','2015-05-21') INSERT INTO #Ranges VALUES (2,'2015-07-12','2015-07-19') INSERT INTO #Ranges VALUES (2,'2015-07-20','2015-07-24') INSERT INTO #Ranges VALUES (2,'2015-07-26','2015-08-02')

With sql find next available integer within range that is not present in existing integer subset(s)

泄露秘密 提交于 2019-12-04 10:13:57
问题 Problem statement: given a range x -> y of unsigned integers where x and y are both in the range 0 -> 2 n and n is 0 -> 32 (or 64 in alternate cases) find the minimum available value not equal to x or y that is not in an existing set where existing sets are arbitrary subsets of x -> y I am working with modeling IPv4 and IPv6 subnets in a database. Each subnet is defined by its starting address and ending address (I ensure the integrity of the ranges via business rules). Because IPv6 is too

How can I get a list of all columns referenced in a stored procedure?

泪湿孤枕 提交于 2019-12-04 09:46:42
问题 I have a number of stored procedures which use CTEs, temp tables, table variables, and sub queries and I need to get the list of all columns (including database, schema, and table/view) used in the stored procedure. I do not need to get the columns in the temp tables, table variables, or CTEs. I just need the referenced columns which are defined in a table or view in a database on my server. I tried sys.dm_sql_referenced_entities and sys.sql_expression_dependencies but they do not return

Is there any releavance for “ROW PER BATCH” AND “MAX INSERT COMMIT SIZE” IN SSIS PACKAGES?

六眼飞鱼酱① 提交于 2019-12-04 09:44:42
问题 I've have SSIS Package that is exporting 2.5 GB OF DATA containing 10 million records into Sql Server Database which has 10 partitions including PRIMARY FILE GROUP. Before Changing default Max Insert Commit size i.e."2147483647" and Row per batch .It was taking 7 mins for completed transformation with fast load option. But After chaning it some decent value with some formula, the execution was done in only 2 minutes. FYI- DefaultMaxBufferRows & DefaultMaxBufferSize were default value in both

Transform vertical result into horizontal mode (T-SQL)

泪湿孤枕 提交于 2019-12-04 09:41:55
Here are the sample data : CalculationDate PLResult 2014-01-02 100 2014-01-03 200 2014-02-03 300 2014-02-04 400 2014-02-27 500 Here are the expected result (in logical format) : January February CalculationDate PLResult CalculationDate PLResult 2014-01-02 100 2014-02-03 300 2014-01-03 200 2014-02-04 400 2014-02-27 500 Here are the expected result (using T-SQL Query) : Jan-CalculationDate Jan-PLResult Feb-CalculationDate Feb-PLResult 2014-01-02 100 2014-02-03 300 2014-01-03 200 2014-02-04 400 2014-02-27 500 Objective: Classify the result according to the month. In the above example, the January

How to protect sql statement from Divide By Zero error

烂漫一生 提交于 2019-12-04 09:22:18
I'm in the process of creating some reports that take a finite total (lets say 2,500) of products (for this example lets say Ice Cream Cones) and counts how many of them were broken before serving. Now the actual count code of broken cones I've got down. SELECT COUNT(broken_cones) FROM [ice].[ice_cream_inventory] WHERE broken_cones = 'Yes' However, I need a percentage of broken cones from this total as well. I've been playing around with the code but I keep running into a 'Divide By Zero' error with this code below. SELECT CAST(NULLIF((.01 * 2500)/Count(broken_cones), 0) AS decimal(7,4)) FROM

Do I need to specify ON DELETE NO ACTION on my Foreign Key?

本秂侑毒 提交于 2019-12-04 09:00:22
问题 I have the following DDL that I am using with SQL Server 2012: CREATE TABLE Subject ( [SubjectId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (50) Not NULL, CONSTRAINT [PK_Subject] PRIMARY KEY CLUSTERED ([SubjectId] ASC) ) CREATE TABLE Topic ( [TopicId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (50) NOT NULL, [SubjectId] INT NOT NULL, CONSTRAINT [PK_Topic] PRIMARY KEY CLUSTERED ([TopicId] ASC) ) ALTER TABLE [Topic] WITH CHECK ADD CONSTRAINT [FK_TopicSubject] FOREIGN KEY([SubjectId])

SQL - Subquery in Aggregate Function

▼魔方 西西 提交于 2019-12-04 08:46:14
问题 I'm using the northwind database to refresh my SQL skills by creating some more or less complex queries. Unfortunately I could not find a solution for my last use case: "Get the sum of the five greatest orders for every category in year 1997." The tables involved are: Orders(OrderId, OrderDate) Order Details(OrderId, ProductId, Quantity, UnitPrice) Products(ProductId, CategoryId) Categories(CategoryId, CategoryName) I have tried the following query SELECT c.CategoryName, SUM( (SELECT TOP 5

SQL Server 2012 can't start because of a login failure

半腔热情 提交于 2019-12-04 07:32:55
问题 I recently installed Microsoft SQL Server 2012 on a fresh Windows 7 installation, but whenever I want to run the server, I get the following error: Error 1069: The service did not start due to a logon failure. The following user is configured to start the service: NT Service\MSSQL$SQLEXPRESS How can I fix this problem? 回答1: The answer to this may be identical to the problem with full blown SQL Server (NTService\MSSQLSERVER) and this is to reset the password. The ironic thing is, there is no