tsql

Unexpected @@rowcount behavior inside an UDF in MS SQL 2019

时光总嘲笑我的痴心妄想 提交于 2020-01-23 01:39:06
问题 here's my sample code drop function rowcount_test go CREATE FUNCTION dbo.rowcount_test () RETURNS INT AS BEGIN DECLARE @v INT SELECT @v = 1 return @@ROWCOUNT END GO grant exec on dbo.rowcount_test to public go SELECT dbo.rowcount_test() It gives 1 when executed by mssql 2017 (and earlier) It gives 0 when executed by mssql 2019 It gives 1 when executed by mssql 2019 (Standard edition) with a db put to the 2017 compatibility mode It's never been a problem before... Is it a kind of setting

Dropping and Adding Link Servers [duplicate]

♀尐吖头ヾ 提交于 2020-01-22 15:18:06
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: SQL Server: Is there an “IF EXISTS” test for a linked server? I am trying to create a block of code that will Create a Link Server/Drop a link server. I had posted similar question about adding and dropping a function and the solution was to drop the function and re-create it. So I want to take the same approach with the LinkServer. DROP it and Re-create it every time I run this code. However, I cannot re-create

Exit and rollback everything in script on error

与世无争的帅哥 提交于 2020-01-22 14:17:07
问题 I have a TSQL script that does a lot of database structure adjustments but it's not really safe to just let it go through when something fails. to make things clear: using MS SQL 2005 it's NOT a stored procedure, just a script file (.sql) what I have is something in the following order BEGIN TRANSACTION ALTER Stuff GO CREATE New Stuff GO DROP Old Stuff GO IF @@ERROR != 0 BEGIN PRINT 'Errors Found ... Rolling back' ROLLBACK TRANSACTION RETURN END ELSE PRINT 'No Errors ... Committing changes'

Flattening hierarchical XML in SQL using the nodes() method

▼魔方 西西 提交于 2020-01-22 12:44:09
问题 I have a Stored Procedure that takes an XML document as a parameter similar in a structure to the following: <grandparent name="grandpa bob"> <parent name="papa john"> <children> <child name="mark" /> <child name="cindy" /> </children> </parent> <parent name="papa henry"> <children> <child name="mary" /> </children> </parent> </grandparent> My requirement is to "flatten" this data so that it can be inserted into a temporary table and manipulated further down the procedure, so the above XML

Flattening hierarchical XML in SQL using the nodes() method

筅森魡賤 提交于 2020-01-22 12:43:46
问题 I have a Stored Procedure that takes an XML document as a parameter similar in a structure to the following: <grandparent name="grandpa bob"> <parent name="papa john"> <children> <child name="mark" /> <child name="cindy" /> </children> </parent> <parent name="papa henry"> <children> <child name="mary" /> </children> </parent> </grandparent> My requirement is to "flatten" this data so that it can be inserted into a temporary table and manipulated further down the procedure, so the above XML

Flattening hierarchical XML in SQL using the nodes() method

◇◆丶佛笑我妖孽 提交于 2020-01-22 12:43:25
问题 I have a Stored Procedure that takes an XML document as a parameter similar in a structure to the following: <grandparent name="grandpa bob"> <parent name="papa john"> <children> <child name="mark" /> <child name="cindy" /> </children> </parent> <parent name="papa henry"> <children> <child name="mary" /> </children> </parent> </grandparent> My requirement is to "flatten" this data so that it can be inserted into a temporary table and manipulated further down the procedure, so the above XML

What's the point to enclose select statements in a transaction?

≯℡__Kan透↙ 提交于 2020-01-22 05:49:05
问题 What's the point to enclose select statements in a transaction? I think select statements are just "GET" data from the database, they don't have chance to rollback something, because you just can't change the data. So, does that to say we never need put select statements in a transaction? Am I right? Thanks. 回答1: You're right: at the standard isolation level, read committed , you do not need to wrap select statements in transactions. Select statements will be protected from dirty reads

Run SQL Server function against each row in table

梦想与她 提交于 2020-01-22 03:43:06
问题 I have a function that which iterates "for each row" in the table. When it is run it should look at each row, pull in the related SET values for that row and run the function which in turns returns a result and updates the correct row with the correct value. What is happening is that it is running and returning the value for the very last row and updating all the row with that value. Any ideas what I'm doing wrong? SELECT RowNum = ROW_NUMBER() OVER(ORDER BY ID) ,* INTO #Geo FROM DDDG DECLARE

How to loop statements in SQL Server

不打扰是莪最后的温柔 提交于 2020-01-22 03:31:11
问题 I am new to SQL Server, I am trying to do something as follows. Sample code : SELECT ITEM_ID FROM 'TABLE_NAME_1' WHERE ITEM_STATUS = 'ACTIVE' SET @ITEM_PRICE = (SELECT PRICE FROM 'TABLE_NAME_2' WHERE 'PRODUCT_ID' = 'ITEM_ID') INSERT INTO 'TABLE_NAME_3' (ITEM_ID, PRICE) VALUES (@ITEM_ID, @ITEM_PRICE) The first statement will return multiple rows of ITEM_ID By using the ITEM_ID I need to select the ITEM_PRICE from another table using the second statement By using the third statement, I need to

Group By clause causing error

别说谁变了你拦得住时间么 提交于 2020-01-22 01:29:05
问题 So here is the context : Developping an ASP.NET MVC 4 web app, I have in my database a table ProductAllocations which is composed of 2 foreign keys : one from my table Products and the other from the table Persons . I have another table, Vehicles which contains a foreign key of the table Products I want to select the allocations and their information grouped by a product (a product can be allocated several time). Here is my stored procedure : ALTER PROCEDURE GetAllVehicles AS BEGIN SET