sql-server

Possible to store value of one select column and use it for the next one?

孤街浪徒 提交于 2021-02-19 08:01:10
问题 Is it possible to store or cache values that are part of one select column and then be used for the next one? For example, select FirstColumn = (complex query returns a value based on ThirdColumn), SecondColumn = (uses the same value returned from above + does some additional calculations.) from SomeTable Is it possible to do something like that so I don't have to write the same complex query twice? 回答1: You need CROSS APPLY here, it can refer to outer references, no annoying subqueries or

SQL Server 2016 with Row Level Security - Addressing the Bottleneck

我的梦境 提交于 2021-02-19 07:59:08
问题 I'm developing with Microsoft SQL Server 2016, and are currently facing a major performance drop when adding Row Level Security (RLS) to my database. I already think I've found the issue, which is Mr Query Optimizer who doesn't like my non deterministic filtering function very much. My question is if anyone had any experience with RLS, filtering functions, and optimizing a case like this. - Can indexing, a more clever RLS filtering function, etc improve the performance? I use RLS to filter

(SQL Server) How to disable foreign key validation during a single INSERT query?

蓝咒 提交于 2021-02-19 07:55:09
问题 I'm trying to improve the performance of a multi-row INSERT query, and the biggest factor at the moment according to the query plan is a FK validation against a large parent table. I know that the INSERT query will not be inserting data that violates the FK, because it is an INSERT INTO ... SELECT ... FROM query where the SELECT involves an INNER JOIN to the parent table on the key columns, so it's not possible that invalid values will be present in the inserted rows. I do not want to disable

SELECT Only one value per ID - SQL Server [closed]

烂漫一生 提交于 2021-02-19 07:33:39
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 1 year ago . Improve this question I dont know is this is possible at all, I´m not even sure how to google it. Im using SQL Server 2014. Right now I have a SP that outputs a table with data including MovementID, Vehicle, VehicleType and Total of sales. But it groups all the vehicles ids and vehicles types with a

Ad hoc updates to system catalogs are not allowed in SQL Server 2012

时光怂恿深爱的人放手 提交于 2021-02-19 06:56:06
问题 I want to remove identity from a column by updating it like this.. sp_configure 'allow update', 1 go reconfigure with override go update sys.syscolumns set colstat = 0 -- turn off bit 1 which indicates identity column where id = object_id('tbl1') and name = 'ids' go sp_configure 'allow updates', 0 go reconfigure go I am getting this error, tried many times. Msg 259, Level 16, State 1, Line 15 Ad hoc updates to system catalogs are not allowed. 回答1: If you want to get rid of it completely, just

Generate Row Number for every 3 rows

你离开我真会死。 提交于 2021-02-19 06:39:09
问题 I want generate number for every three rows CREATE TABLE #test(period INT) INSERT INTO #test VALUES (602),(603),(604),(605),(606),(607),(608),(609) I know we can generate sequence using row_number window function or while loop or cursor SELECT period, ( Row_number()OVER(ORDER BY period) - 1 ) / 3 + 1 FROM #test Result; +--------+-----+ | period | seq | +--------+-----+ | 602 | 1 | | 603 | 1 | | 604 | 1 | | 605 | 2 | | 606 | 2 | | 607 | 2 | | 608 | 3 | | 609 | 3 | +--------+-----+ Is there any

With ADO, using SqlDataAdapter.FillSchema clears @ReturnValues and other stored procedure parameters marked for output

你。 提交于 2021-02-19 06:15:53
问题 [Edit - an explanation of the answer is at the foot of the question because the subtlety is not totally obvious from the accepted answer.] Original question: I am looking for a quick 'yes this approach should be possible' answer before I commit further effort. Scenario: I have a simple SQL Server stored procedure that runs a query. I also want to (1) return a varchar(100) parameter declared as OUTPUT , and (2) use the @ReturnValue to return a further (integer) value. I call this procedure

Change the file encoding of the file which is created using SSIS Log provider for Text Files

给你一囗甜甜゛ 提交于 2021-02-19 05:41:14
问题 I am new to SSIS, I have already designed a package and configured SSIS Log provider for Text Files. This works fine and log files are generated successfully. We have a monitoring team, they use this log file for monitoring. They are unable to read the log files since the file encoding is in Unicode format. They are expecting a non unicode format for their monitoring. I tried to change the existing log file encoding to ANSI but when I re-run the package my log file has been created again with

Is there a way to simplify a NULL compare of 2 values

懵懂的女人 提交于 2021-02-19 05:38:06
问题 This is my simplified statement SELECT ... FROM tab1 AS i FULL OUTER JOIN tab2 AS d ON i.[Id]=d.[Id] WHERE d.[Data]<>i.[Data] OR (d.[Data] IS NULL AND i.[Data] IS NOT NULL) OR (d.[Data] IS NOT NULL AND i.[Data] IS NULL) I want to get all entries that are i.[Data] is different from d.[Data] At least one value in table i or d is NOT NULL So I don't want to see records were and i and d contain the same data or are both NULL. My statement look so long and complicated. Is there an easier way?

Allow login to run stored procedure without being able to select from table

非 Y 不嫁゛ 提交于 2021-02-19 05:33:44
问题 I have a SQL Server with two databases: Database1 Database2 Login 'MyLogin' has read access on Database2 only. Database2 has a stored procedure as follows: CREATE PROCEDURE Get_LogData @ProductID int AS BEGIN If Exists (Select (1) From Database2.dbo.Products Where ProductID = @ProductID) Select LogTimeStamp , ProductID , Description from Database1.dbo.Log Where ProductID = @ProductID Else Print 'Get_LogData: Unable to find ProductID ' + CAST(@ProductID AS VARCHAR) END; GO BEGIN GRANT EXEC ON