tsql

SQL Grouping Acct Numbers and Getting Subtotals and Grand Total

梦想的初衷 提交于 2020-01-17 08:11:05
问题 This query... SELECT ACTNO,SUM(PLAN) AS 'TOTAL PLAN', SUM(FORECAST) AS 'TOTAL FORECAST' FROM COST_CENTER GROUP BY ACTNO I get the following results: ACTNO | TOTAL PLAN | TOTAL FORECAST 100 | 12000 | 20000 101 | 12000 | 20000 200 | 1000 | 1500 201 | 1200 | 2000 202 | 900 | 1500 203 | 700 | 1500 220 | 1500 | 3500 300 | 1200 | 2000 301 | 1000 | 3000 I need to group together certain accounts, get those groups' subtotals, and then add all the subtotals up for a grand total. The desired result

combining Geography data

Deadly 提交于 2020-01-17 05:09:11
问题 Using something like this works: DECLARE @Shape GEOGRAPHY = 'POLYGON EMPTY' SET @Shape.STSrid = 4326; SELECT @Shape = @Shape.STUnion([PolygonData]) FROM [Bla].[Polygon] SELECT @Shape.STAsText(); on a table like this: CREATE TABLE [Space].[Polygon]( [Id] [UNIQUEIDENTIFIER] NOT NULL, [PolygonData] [GEOGRAPHY] NULL) Unfortunately, it is very slow. I do not think I can index this so I wonder if there are alternative approaches that can be used to merge polygons to obtain just the outer points of

Using T-SQL EXCEPT with DELETE / Optimizing a query

安稳与你 提交于 2020-01-17 04:44:26
问题 The following code removes records of tasks related to inactive projects from the table. delete from [Deliverables] where [Deliverables].[ProjectID] not in ( select [ProjectID] from [ActiveProjects] ) I've read somewhere that using NOT IN with subquery that returns a lot of values is not really the most efficient thing to do and it's better to use EXCEPT clause. However, when I try to use the following code, I get an error (Incorrect syntax near the keyword 'except'.) delete from

Whilst locked - see if otherID exists, if so return mainID, if not insert otherID & return new mainID

独自空忆成欢 提交于 2020-01-17 04:43:25
问题 I need to check if a unique int - otherID (not the primary key) exists in a table. If it does, return the primary key. If it doesn't, insert a record containing the otherID I checked, then return the new primary key. ID unqiueidentifier, --PK name varchar(100), otherID int --Unique I need to holdlock whatever statement I use because between checking and inserting a concurrent user could insert the same otherID. I was looking at using a MERGE with holdlock - but it seems that can only be used

Whilst locked - see if otherID exists, if so return mainID, if not insert otherID & return new mainID

六月ゝ 毕业季﹏ 提交于 2020-01-17 04:43:04
问题 I need to check if a unique int - otherID (not the primary key) exists in a table. If it does, return the primary key. If it doesn't, insert a record containing the otherID I checked, then return the new primary key. ID unqiueidentifier, --PK name varchar(100), otherID int --Unique I need to holdlock whatever statement I use because between checking and inserting a concurrent user could insert the same otherID. I was looking at using a MERGE with holdlock - but it seems that can only be used

What is the difference between how the “Data” and “Preview” data generation works in BIDS?

对着背影说爱祢 提交于 2020-01-17 03:21:09
问题 When I select the "!" ("Run") button on the Data tab of the design surface in a BIDS project, it prompts me for the parameters the Stored Procedure I have selected needs: Once these are supplied, and I click the Okay button, I see the data returned in a grid within the Data tab. Using the same Stored Procedure (but not selected exlicitly - apparently the project knows which one to use based on what is selected in the Data tab) on the Preview tab, instead of prompting me for the Stored

Generate insert statement with data in sql server

烂漫一生 提交于 2020-01-17 03:04:06
问题 I searched & got this below stored procedure to generate insert statement with data. The problem is this stored procedure can not handle single quotes. Suppose if there is any single quote in data then the insert statement generates but when execute those insert statements then I get an error. So guide me where to fix in procedure as a result it can handle any single quote or any special character. Thanks CREATE PROC InsertGenerator (@tableName varchar(100)) as --Declare a cursor to retrieve

Replace multiple instance of a character with a single instance in sql

半城伤御伤魂 提交于 2020-01-17 01:31:27
问题 Probably it's a duplicate, but I couldn't find a solution. Requirement: I have the below strings: Heelloo Heeelloo Heeeelloo Heeeeeelloo Heeeeeeeelloo . . . Heeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelloo Expected output: Hello What is the best way to achieve this in SQL? Version I am using : Microsoft SQL Server 2012 - 10.0.7365.0 (X64) Jul 28 2015 00:39:54 Copyright (c) Microsoft Corporation Parallel Data Warehouse (64-bit) on Windows NT 6.2

Parameter functioning in SSMS but not SSRS

放肆的年华 提交于 2020-01-17 00:53:41
问题 Assume I have the following query (simplified version of what im really using)... DECLARE @p_ServerName nvarchar(10) SELECT SystemName FROM Table_x WHERE SystemName = @p_ServerName OR @p_ServerName = 'all' When I set the parameter to a specific name ex: DB1 the rows are displayed in SSRS but when I input 'all' as the parameter, nothing is displayed. Yet when I do the same in SSMS it works just fine. Does anyone know what can be the problem here? Thanks. EDIT Here is the first query I tried

T-SQL table name concatenate with string and @parameter

寵の児 提交于 2020-01-17 00:34:36
问题 I want to get a count of my @sql before it execute. But I cannot concatenate table name with my parameter value. SQL: SET @count = (SELECT count(*) as tblcount FROM RDL_tbl_UserManagement_UserInfo_ + @table_prefix_name WHERE partnerID = CAST(@partnerID AS VARCHAR) AND userID = CAST(@adminUserID AS VARCHAR) AND addressType = 'H') IF @count = 0 BEGIN SET @sql = 'SELECT * FROM RDL_tbl_UserManagement_UserInfo_' + @table_prefix_name + ' WHERE partnerID = ' + CAST(@partnerID AS VARCHAR) + ' AND