sql-server-2012

TSQL Sum by group for monthly reports

别等时光非礼了梦想. 提交于 2019-12-06 07:10:15
I'm in the process of trying to produce a report (monthly, year to date, etc) on a group of shipping entries in our database to show correct sums as well as group them by shipping company. How can I accomplish taking all this information and produce proper reports? One thing to note is that all of the entries in this table are nvarchar(max) due to text importing with odd characters from various sources. The master database list looks something like this: table - shipping_info shipping_company | ship_date | shipping_category | shipping_cost UPS | 20130301 | CD'S | 3.50 UPS | 20130310 | Records

SQL Cross Tab Query

霸气de小男生 提交于 2019-12-06 07:02:24
Need help figuring out how to do a cross-tabulated report within one query. There are 3-4 tables involved but the users table may not need to be included in the query since we just need a count. I have put together a screenshot of the table schema and data as an example which can be seen below: What I need it to return is a query result that looks like: So I can make a report that looks like: I've tried to do cursor loops as it's the only way I can do it with my basic knowledge, but it's way too slow. One particular report I'm trying to generate contains 32 rows and 64 columns with about 70

Updating table with business day and calendar day

岁酱吖の 提交于 2019-12-06 06:31:06
I have a table in SQL Server 2012 that is updated manually every month to reflect what date is a file expected to come in. The date rule already has values but the expected date column is what is updated manually. If its expected on BD1(Business Day 1) I will update to the first non-weekend day of the month. If its expected on CD1(Calenday Day 1) I will update to the 1st regardless if it falls on a weekday or a weekend and so forth. Is it possible to write an update query where it would loop through the values and update automatically? I'm having trouble figuring out to update to the correct

Passing multiple values to a parameter of a function in SQL

有些话、适合烂在心里 提交于 2019-12-06 06:03:11
问题 There is function Getfunctionname(userid, startdate, enddate) to return a table My question is can I pass a variable with multiple values? i.e. getfunctionname(@userid, startdate, enddate) Where the value of variable @userid is like 1 2 3 4 5 (actually using split function splitting the values from being 1,2,3,4,5 ) If I can please let me know 回答1: One way of doing that which I prefer is to make a new user-defined table data type. CREATE TYPE [dbo].[IdList] AS TABLE( [Id] [int] NULL ) Then

Error while installing SQL Server 2012 Standard Edition

半腔热情 提交于 2019-12-06 06:00:06
Here's how the error looks while the installation: An error occurred during the setup process of the feature Attempted to perform an unauthorized operation. Clicking on Retry doesn't help, if I click on the Cancel button, it continues but then there will come up more errors like this one: NOTE: I'm not using any VM, I'm using Windows 7 Ultimate 32 bit as my OS. Here's the log details: Overall summary: Final result: Failed: see details below Exit code (Decimal): -2068119551 Start time: 2013-10-18 14:59:09 End time: 2013-10-18 16:02:02 Requested action: Install Setup completed with required

How To check minimum Date in each observation and update flag dynamically

若如初见. 提交于 2019-12-06 05:40:36
I have to check for minimum date each time and add 30 days to it and set the field accordingly. For Example: My Table: ID StartDate EndDate 1 2017-01-01 2017-02-01 1 2017-01-09 2017-01-28 1 2017-04-01 2017-04-30 1 2017-04-05 2017-05-20 1 2017-04-20 2017-06-12 2 2017-06-02 2017-06-20 2 2017-06-14 2017-07-31 2 2017-06-14 2017-07-31 2 2017-06-19 2017-07-31 2 2017-06-19 2017-07-31 so, here min(startdate) is 2017-01-01. Now I add 30 days to it and my look up date is 2017-01-31. for any record which has start date less than this look up date I have to set flag_1. My table will look like: ID

Use of temporary functions or procedure within a script

隐身守侯 提交于 2019-12-06 05:39:16
I am using SQL Server 2012 , i have a script by which i am inserting values to a table, in that script i have to convert the format of some DateTime variables on the basis of two parameters. I can do it using CASE or if condition in sql . I am not allowed to make any Function or procedure in the database to which i can refer. Is there any other way like creating a temporary function or temporary procedure within the script to apply condition alter the format for Datetime values? Yes you can: CREATE PROCEDURE #usp_TempOne @Input INT, @Output INT OUTPUT as SET @Output = @Input * 2 RETURN GO

Error when Update-Database using Code-First: “There is already an object named 'something' in the database.”

夙愿已清 提交于 2019-12-06 04:56:07
I'm doing database migration using Code-First in Visual Studio 2015. Along the way, i've done the migration steps up till Add-Migration . After Add-Migration, i've added this line of code Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of DbContext1, Migrations.Configuration)) in my DbContext constructor to set up Database Initializer since i missed this step previously. After that, i executed "Add-Migration initial -Force" in the Package Manager Console because i fear that this part is needed in the Add-Migration process. Then, i directly executed "Update-Database" The problem is

Open and Close Cursors Inside or Outside a Transaction and How to Close a Cursor if a Transaction Fails

こ雲淡風輕ζ 提交于 2019-12-06 03:55:51
问题 I am writing a stored procedure in SQL Server 2012 that uses a cursor for reading and a transaction inside a TRY CATCH block. Basically, my questions are as follows: Should I declare my cursor inside the TRY CATCH block? If yes, should I declare the cursor before or after the BEGIN TRANSACTION statement? Should I open the cursor before or after the BEGIN TRANSACTION statement? Should I close and deallocate the cursor before or after the COMMIT TRANSACTION statement? Should I close and

sp_send_dbmail fails with attachment

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 03:25:15
问题 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 =