sql-server-2012

T-SQL join table only when the table is not empty

邮差的信 提交于 2019-12-12 12:14:14
问题 I have the following tables with one column (RecordID): TableOne 101 102 103 104 105 106 TableTwo 101 102 103 104 and want to make join between them only when TableTwo is not empty. This could be done with sample IF statement, but in my real situation this would lead to a lot of code duplication. I have try the following: SELECT * FROM TableOne T1 WHERE exists (select 1 from TableTwo where T1.RecordID=RecordID) and exists (select 1 from TableTwo) using this answer , but the same logic is not

DbFunction “cannot be translated into a LINQ to Entities store expression”

北城以北 提交于 2019-12-12 12:05:42
问题 I'm trying to access database funciton using linq to sql. Herer is my SQL Scalar Function: CREATE FUNCTION Echo(@text NVARCHAR(MAX)) RETURNS NVARCHAR(MAX) AS BEGIN RETURN @text; END; I created a class called EntityFunction to call Functions in Sql Server: public static class EntityFunctions { [DbFunction("SqlServer", "Echo")] public static string Echo(string parameter) { throw new NotImplementedException(); } } And here is my DbContext: public class MainDbContext : DbContext { #region

Return 1 instead of 0 when Count(*) result is Null

与世无争的帅哥 提交于 2019-12-12 11:28:42
问题 My code from SQL Server: SELECT ESTAGIO.SK_ESTAGIO, ISNULL(count(ESTAGIO.SK_ESTAGIO), 0) as how_many from ESTAGIO left join ESTAGIARIO on ESTAGIARIO.SK_ESTAGIO = ESTAGIO.SK_ESTAGIO group by ESTAGIO.SK_ESTAGIO When "ESTAGIO.SK_ESTAGIO" doesn't exist in the table "ESTAGIARIO" it returns 1 instead of 0, I already tried to use ISNULL(), NULLIF() and COALESCE() and still couldn't find the problem that is making the query above returning 1 when it should be 0. 回答1: You are counting the wrong field.

Insert a empty string on SQL Server with BULK INSERT

試著忘記壹切 提交于 2019-12-12 11:24:21
问题 Example table contains the fields Id (the Identity of the table, an integer); Name (a simple attribute that allows null values, it's a string) I'm trying a CSV that contains this: 1, 1,"" 1,'' None of them gives me a empty string as the result of the bulk insertion. I'm using SQL Server 2012. What can I do? 回答1: As far as I know, bulk insert can't insert empty string, it can either keep null value or use default value with keepnulls option or without keepnulls option. For your 3 sample

Pass List of strings to a stored procedure [duplicate]

假如想象 提交于 2019-12-12 10:30:46
问题 This question already has answers here : C# SQL Server - Passing a list to a stored procedure (8 answers) Closed 4 years ago . Using, SQL Server 2012, I would like to create a stored procedure, that passes in a list of string and checks it for each entry.Iv added the list to one comma separated string 'UserGroupsAllowedToViewMap'. This was working for one entry but I need to check it for a number of entries. public DataTable GetMapsWithWorkspaceForUserGroups(int workspaceID, string

fulltext index returning no results from pdf filestream

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:47:07
问题 I have a filestream table running on SQL Server 2012 on a Windows 8.1 x64 machine, which already have a few PDF and TXT files stored, so I decided to create a fulltext index to search through these files by using the following command: CREATE FULLTEXT CATALOG FileStreamFTSCatalog AS DEFAULT; CREATE FULLTEXT INDEX ON storage (FileName Language 1046, File TYPE COLUMN FileExtension Language 1046) KEY INDEX PK__storage__3214EC077DADCE3C ON FileStreamFTSCatalog WITH CHANGE_TRACKING AUTO; Then I

SELECT query with multiple sub-queries for counts

那年仲夏 提交于 2019-12-12 09:46:24
问题 I just wrote this query for a report. But I originally wrote it without the date range filter on every sub-query. But that didn't work. So I added it to each sub-query. And that worked, but I don't really like having to repeat it every time, is there syntax to do the same thing simpler? SELECT Count(r.id) AS cnt_total, (SELECT Count(r1.entity_id) FROM auto_reminders_members r1 WHERE r1.reminder_id = r.reminder_id AND r1.date_last_reminder BETWEEN CONVERT(DATETIME, '03/28/2013', 101) AND

How can I use LTRIM/RTRIM to search and replace leading/trailing spaces?

给你一囗甜甜゛ 提交于 2019-12-12 08:47:28
问题 I'm in the process of trying to clear out leading and trailing spaces from an NVARCHAR(MAX) column that is filled with prices (using NVARCHAR due to data importing from multiple operating systems with odd characters). At this point I have a t-sql command that can remove the leading/trailing spaces from static prices. However, when it comes to leveraging this same command to remove all prices, I'm stumped. Here's the static script I used to remove a specific price: UPDATE *tablename* set

SQL - how do I generate rows for each month based on date ranges in existing dataset?

你离开我真会死。 提交于 2019-12-12 08:12:41
问题 assume I have a dataset: rowID | dateStart | dateEnd | Year | Month 121 | 2013-10-03 | 2013-12-03 | NULL | NULL 143 | 2013-12-11 | 2014-03-11 | NULL | NULL 322 | 2014-01-02 | 2014-02-11 | NULL | NULL And I want sql to generate the following datasource based on the dateStart and the dateEnd. Note the year and month grouping. rowID | dateStart | dateEnd | Year | Month 121 | 2013-10-03 | 2013-12-03 | 2013 | 10 121 | 2013-10-03 | 2013-12-03 | 2013 | 11 121 | 2013-10-03 | 2013-12-03 | 2013 | 12

easiest way to add a SQL column through VB

眉间皱痕 提交于 2019-12-12 06:55:59
问题 What i want to do is first check if a certain column already exists in a table and if not add it. I want to implement this through visual basic. If somebody took a little time to comment and briefly explain each step i would greatly appreciate it. 回答1: There are two ways to determine if a column exists: either try to use it and catch the error if it doesn't exist, or read the metadata from the database see SQL Server: Extract Table Meta-Data (description, fields and their data types) Once you