sql-server-2012

How to update the column without loop in SQL Server?

社会主义新天地 提交于 2019-12-11 01:51:22
问题 Due to performance perspective I just need to remove loop and using some joins or other solution to update the data in #Result table and get the same result which return by loop. Scalar function: CREATE FUNCTION [MultiplyerScl] (@a INT, @b INT) RETURNS INT AS BEGIN DECLARE @i AS INT = 2 DECLARE @Value AS INT IF (@b % @a = 0) BEGIN SET @Value = @b END ELSE BEGIN WHILE (1=1) BEGIN IF((@b * @i) % @a = 0) BEGIN SET @Value = @b * @i BREAK; END ELSE BEGIN SET @i = @i + 1 END END END RETURN @Value

Unable to connect to SQL LocalDB

浪子不回头ぞ 提交于 2019-12-11 01:45:58
问题 I have a native C++ app within which I am trying to connect to a localdb instance using ADO. Having manually started my instance I can run sqllocaldb info v11.0 and see the database instance is running. My code is as follows. ADO::_ConnectionPtr spConnection (__uuidof (ADO::Connection)); spConnection->Open (L"Provider=SQLNCLI11;Server=(localdb)\\v11.0;Integrated Security=true", L"", L"", 0); The error code is DB_E_ERRORSOCCURRED (0x80040e21) and the error message is Multiple-step OLE DB

Store totals or calculate on the fly?

好久不见. 提交于 2019-12-11 01:37:32
问题 I have many tables in my database that hold information on items (Photos, Articles, Videos) that can be Liked, Disliked, Shared, Favourited etc by users. Each time a user takes an action on a item, it is recorded in a simple table like such: ItemID | UserID | Liked | Shared | Favourited 1 1 NULL 1 NULL 2 25 1 1 1 3 18 0 NULL NULL When I'm writing a query to return a list of items from a table (e.g. Photos) I also want to return the total number of Likes, Shares etc each item has. At the

Using a temporary table in dynamic sql in a stored procedure

只谈情不闲聊 提交于 2019-12-11 01:29:08
问题 I am writing a Store Procedure in SQL Server 2012. I have a temporary table defined like so: DECLARE @CURRENT_RET_WEEK_PTIMEIDS TABLE ( PTIMEID INT ) I am also using EXECUTE to write a dynamic SQL query. Is there any way I can join this table onto the above temporary table? 回答1: Try to use local temp-table - IF OBJECT_ID ('tempdb.dbo.#temp') IS NOT NULL DROP TABLE #temp CREATE TABLE #temp (ID INT) INSERT INTO #temp (ID) VALUES (1),(2) DECLARE @SQL NVARCHAR(MAX) SELECT @SQL = 'SELECT * FROM

Replace all characters of string to asterisks except first characters and space in MSSQL

余生长醉 提交于 2019-12-11 01:26:34
问题 I`m was wonder if it is possible to hash sensitive data in mssql via function and to leave first character “as-is” for all the fields. Example: "Jon Kirk” name should be extracted as J** K*** 回答1: I had posted a recursive solution at first. This is faster: declare @name varchar(20) = 'Jon Kirk' declare @loop int = len(@name) while @loop > 1 select @name = stuff(@name, @loop, 1, case when substring(@name, @loop-1,2) like '% ' then ' ' when substring(@name, @loop-1,2) like ' %' then substring(

SQL performance issue how to modify the below Script?

◇◆丶佛笑我妖孽 提交于 2019-12-11 01:12:50
问题 I have written a SQL query that involves INNER JOIN, LEFT OUTER JOIN, UNION and a subquery which is taking a long time to execute. My SQL skills are not that great and I would appreciate if someone can help me how can I optimize the following query to make it faster. SELECT Brand, Quantity, RegionNumber, FinancialGroup, CustomerLocationNumber, RecipientCode, Company, Contact, Address1, Address2, City, [State], Zip, Country, SUM(CurrentYearSales) 'CurrentYearSales', SUM(PriorYearSales)

Select random rows and stop when a specific sum/total is reached

狂风中的少年 提交于 2019-12-11 01:04:14
问题 I'm using SQL Server 2012 and I'm trying to do something like this: SELECT SUM(MILES) from tblName WHERE mDate > = '03/01/2012' and mDate <= '03/31/2012' -- and... /* now I want to add here do until the SUM of Miles is equal to or greater then '3250' and get the results rows randomly */ So in other words, I want to select random rows from a table that have a specified from and to date and stop when the sum of miles is at or over the number: 3250 回答1: Since you're using SQL Server 2012, here

Performance Tuning SQL

丶灬走出姿态 提交于 2019-12-11 00:47:11
问题 I have the following sql. When I check the execution plan of this query we observe an index scan. How do I replace it with index seek. I have non clustered index on IdDeleted column. SELECT Cast(Format(Sum(COALESCE(InstalledSubtotal, 0)), 'F') AS MONEY) AS TotalSoldNet, BP.BoundProjectId AS ProjectId FROM BoundProducts BP WHERE BP.IsDeleted=0 or BP.IsDeleted is null GROUP BY BP.BoundProjectId I tried like this and got index seek, but the result was wrong. SELECT Cast(Format(Sum(COALESCE

Price compare with multiple store in sql

你说的曾经没有我的故事 提交于 2019-12-11 00:38:43
问题 create table Products ( id int, ProductName varchar(200), ProductCategory varchar(200), ProductImage varchar(200), ProductUri varchar(200), ) Insert into Products values(135, 'Product X', 'Digital Camera', Null, Null) Insert into Products values(136, 'Product Y', 'Mobile', Null, Null) create table Product_Price ( id int, ProductId int, dt date, SellerName varchar(20), Available varchar(20), Offer varchar(20), Price money, Shipping money ) insert into Product_Price values (1, 135,'2012-01-16',

How to get total number of rows in a executed select statement? [duplicate]

半腔热情 提交于 2019-12-10 23:49:35
问题 This question already has answers here : How to include the total number of returned rows in the resultset from SELECT T-SQL command? (7 answers) Closed 5 years ago . How can I out how many rows I obtained after execution? My query is: SELECT a.Emp,b.orders from table as a inner join table1 b on a.ID = B.ID How do I find the number of rows returned in the above join? 回答1: You either have to use SELECT COUNT(*) ... with the same condition or add a column with the row-count via ROW_NUMBER