tsql

Unable to Create Multiple User Defined Functions in SQL Server Using System.Data.SqlClient

岁酱吖の 提交于 2020-04-30 08:33:14
问题 I am trying to create multiple user defined functions from within the same .sql file. I am using SQL Server and am executing my queries using the SqlClient from C#'s System.Data . Contents of the .sql file: CREATE FUNCTION [dbo].[GetUserId] (@username VARCHAR(32)) RETURNS INT AS BEGIN DECLARE @userId INT = -1 SET @userId = (SELECT DISTINCT UserId FROM Users WHERE UserName = @username) RETURN @userId END GO CREATE FUNCTION [dbo].[GetUserId2] (@username2 VARCHAR(32)) RETURNS INT AS BEGIN

sql server round function not working well

时光总嘲笑我的痴心妄想 提交于 2020-04-30 07:41:59
问题 I am using sql server 2000 and facing round function issue like the following statement working fine. SELECT ROUND(5 * 7.83, 1) The result will be 39.2 But when I get these values from the table, it gives 39.1, meaning it truncates and does not round up. SELECT ROUND(rate * qty, 1) FROM tbl The result will be 39.1 rate and qty columns data types are float. Insert 5 in qty and 7.83 in rate , then check it. How I can fix it? 回答1: Convert the table values to real, SELECT ROUND(convert(real,rate)

sql server round function not working well

情到浓时终转凉″ 提交于 2020-04-30 07:41:22
问题 I am using sql server 2000 and facing round function issue like the following statement working fine. SELECT ROUND(5 * 7.83, 1) The result will be 39.2 But when I get these values from the table, it gives 39.1, meaning it truncates and does not round up. SELECT ROUND(rate * qty, 1) FROM tbl The result will be 39.1 rate and qty columns data types are float. Insert 5 in qty and 7.83 in rate , then check it. How I can fix it? 回答1: Convert the table values to real, SELECT ROUND(convert(real,rate)

Is branch pruning possible for recursive cte query

 ̄綄美尐妖づ 提交于 2020-04-30 06:38:26
问题 This is inspired by question Retrieve a list of lists in one SQL statement - I have come up with a solution, but I have doubts on its efficiency. To restate the problem: we have 2 Tables: Person and Parent Person contains basic data about each person Parent is a join table relating person with its parents each Person can have multiple parents we want to receive each person data with list of all their ancestors - each ancestor in its own row if there are no ancestors, we have only one row for

Error when trying to execute a command in sql server

前提是你 提交于 2020-04-20 18:40:47
问题 I am using SQL Server 2014 and working on creating views from all the user database on to a Test database. The generated sql statement works fine when run from SSMS but when the execute command tries to run it I get the following error message. SQL Query: DECLARE @SQL NVARCHAR(MAX), @QueryStmt NVARCHAR(MAX) SET @SQL = '' SELECT @SQL = @SQL + CHAR(13) + 'USE ' + QUOTENAME([name]) + '; SELECT @QueryStmt = ''USE TestDWH ''+ CHAR(13) + CHAR(10) + ''GO'' + CHAR(13) + CHAR(10) + ''IF EXISTS (SELECT

Error when trying to execute a command in sql server

痴心易碎 提交于 2020-04-20 18:37:29
问题 I am using SQL Server 2014 and working on creating views from all the user database on to a Test database. The generated sql statement works fine when run from SSMS but when the execute command tries to run it I get the following error message. SQL Query: DECLARE @SQL NVARCHAR(MAX), @QueryStmt NVARCHAR(MAX) SET @SQL = '' SELECT @SQL = @SQL + CHAR(13) + 'USE ' + QUOTENAME([name]) + '; SELECT @QueryStmt = ''USE TestDWH ''+ CHAR(13) + CHAR(10) + ''GO'' + CHAR(13) + CHAR(10) + ''IF EXISTS (SELECT

Error when trying to execute a command in sql server

▼魔方 西西 提交于 2020-04-20 18:36:54
问题 I am using SQL Server 2014 and working on creating views from all the user database on to a Test database. The generated sql statement works fine when run from SSMS but when the execute command tries to run it I get the following error message. SQL Query: DECLARE @SQL NVARCHAR(MAX), @QueryStmt NVARCHAR(MAX) SET @SQL = '' SELECT @SQL = @SQL + CHAR(13) + 'USE ' + QUOTENAME([name]) + '; SELECT @QueryStmt = ''USE TestDWH ''+ CHAR(13) + CHAR(10) + ''GO'' + CHAR(13) + CHAR(10) + ''IF EXISTS (SELECT

SQL Function to split string and concat substrings

喜你入骨 提交于 2020-04-18 06:14:07
问题 I am trying to create a function that will take in a column from a table, for each string in a cell it will split in to separate words and concat each word separatley to create different substrings. I have been looking at how to manage it with SQL like arrays et but I have had no luck The two tables are : Account(AccountID(PK), Name, Country) accountSubstring(subID(PK), AccountID, Substring) Ideally I need a function that would take in the column 'Name' from Account. For each row it would

SQL Function to split string and concat substrings

萝らか妹 提交于 2020-04-18 06:13:13
问题 I am trying to create a function that will take in a column from a table, for each string in a cell it will split in to separate words and concat each word separatley to create different substrings. I have been looking at how to manage it with SQL like arrays et but I have had no luck The two tables are : Account(AccountID(PK), Name, Country) accountSubstring(subID(PK), AccountID, Substring) Ideally I need a function that would take in the column 'Name' from Account. For each row it would

Iterate through columns and list all columns where a record has a value

元气小坏坏 提交于 2020-04-18 05:41:26
问题 I have the following table: ID Group Col1 Col2 Col3 Col4 ... ColN ------------------------------------------------------ 1 AAA foo bar 2 AAA 123 far baz 3 BBB 4 CCC 345 123 5 AAA caz For each Group , I need to find out in what Col umns it has a value. I do not care about the values themselves. Example: Group AAA has 3 ID s in it: 1, 2, 5 . ID 1 has a value in Col1, Col2 . ID 2 has a value in Col1, Col2 . ID 5 has a value in Col3 so in total, Group AAA has values in Col1, Col2, Col3 The output