tsql

Not able to compare columns in SQL Server

柔情痞子 提交于 2020-01-16 10:03:27
问题 I am using the following SQL code to compare two nvarchar columns. But the code is showing incorrect results: SELECT DP.NAME, DP.sid, SU.sid, CASE WHEN DP.sid = SU.sid THEN 'TRUE' ELSE 'FALSE' END AS DESDIREDRESULT FROM #SQLLOGINS DP INNER JOIN SYS.sysusers SU ON DP.name COLLATE DATABASE_DEFAULT = SU.name COLLATE DATABASE_DEFAULT In the code, I am doing a inner join of a temp table, #SQLLOGINS , with sys.sysusers . This temp table includes NAME and SID of sys.sqllogins . I am facing an issue,

Reduce cost for Table Valued Function - XML Reader in query plan - how?

你说的曾经没有我的故事 提交于 2020-01-16 09:43:10
问题 I have the below SQL Server query: with cte as (SELECT DISTINCT refrecid FROM docuref WHERE ACTUALCOMPANYID = 'an' and REFTABLEID='78' and typeid='Note') SELECT docuref.REFRECID, Notes = STUFF ((SELECT CHAR(13) + CHAR(10) + cast([NOTES] AS nvarchar(max)) FROM DOCUREF WHERE REFRECID = Cte.refrecid AND ACTUALCOMPANYID = 'an' FOR XML PATH(''), TYPE ).value('.', 'nvarchar(max)'), 1, 2, '') FROM Cte INNER JOIN DOCUREF ON cte.REFRECID= docuref.REFRECID WHERE DOCUREF.ACTUALCOMPANYID = 'an' and

GROUP_CONCAT in SQL Server error

扶醉桌前 提交于 2020-01-16 09:06:08
问题 Error:Column 'ReviewConsultants.ConsultantID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Query: select R.ReviewID, STUFF((select distinct ',' + FirstName from Users where UserID = RC.ConsultantID FOR XML PATH ('')), 1, 1, '') AS consultantlist from [Reviews] R, [ReviewConsultants] RC where R.ReviewID = RC.ReviewID group by R.ReviewID; One review can have one or more consultants.I am trying to get the consultants for each

TSQL query for tree view control and remove empty elements from an xml file

寵の児 提交于 2020-01-16 07:31:47
问题 Query for treeview control, sometimes contains empty elements which throws an exception (when there are no attributes in an element) due to data-binding of the control the text values is set to "GrandChildOfFirstRow" I did get rid of them in my query via xquery but is there an alternative way to doing this or a better smarter way to get rid of those empty elements, (I need the left outer join for proper records for this query) or is it possible to combine the xquery code into shorter code:

Checking a SQL job status in C# using sp_help_job

泄露秘密 提交于 2020-01-16 04:40:10
问题 This similar question gave the solution that in order to check the status of a job in C# you must run sp_help_job. The problem is when I try the query out in SQL, I'm getting an error from SQL Server saying "Could not find stored procedure 'sp_help_job'. I tried running this SQL code in MSS 2008: exec sp_help_job Can someone post the exact query to find out the status of a job I just ran? 回答1: If you are writing C# code, the easiest solution would probably be to use the SMO API to get the

SQL syntax error 170 in insert statement

陌路散爱 提交于 2020-01-16 04:32:07
问题 here is my SQL statement and I am getting Error 170 Incorrect syntax near ',' at line 4 INSERT INTO SEO_Permalink_Test ( IDObjekt ,IDType ,IDLanguage ,StateSEOName ,StateSEOPermalink ,DatumErstellungSEOName ,DatumLetzteAenderungSEOName ,SEOName) VALUES ( 19988 , 72 , 1 , 0 , 0 , GETDATE() , GETDATE() , 'H_KDA1559_ST_004' ), ( 19989 , 72 , 1 , 0 , 0 , GETDATE() , GETDATE() , 'H_KDA1559_FS_003' ) , ( 19997 , 72 , 1 , 0 , 0 , GETDATE() , GETDATE() , 'H_CAU0171_WO_015' ) , ( 19998 , 72 , 1 , 0 ,

ASP - SQL injection protection in the SELECT clause

浪子不回头ぞ 提交于 2020-01-16 04:04:16
问题 After getting great help in securing against SQL injection from classic ASP protection against SQL injection, I've encountered a major issue which cannot be solved using parameterized queries. name = Trim(Request.QueryString("name")) flds = Trim(Request.QueryString("flds")) sql = "set rowcount 0 select " & flds & " from [TABLE] where Name = '" & name & "'" From what I understand, a parameterized query will protect against SQL injection in the WHERE clause (in this case, the name field. flds

Recursive query to find the parent record

▼魔方 西西 提交于 2020-01-16 04:02:06
问题 Based on the highest level and for the corresponding childID record i.e. 71 here, I need to go up the level all the way to 1 and get the corresponding childID record i.e. 209 For ex: To find the childrecord for 71: level4 parent - 154, level3 parent - 192, level2 parent - 209 or level1 child - 209 209 is the needed answer. Now the tricky part is that the highest level is variable. My query shown above doesn't work as the level increases to 6 or 7 as I will not know the number of joins needed.

TSQL not using indexes

爱⌒轻易说出口 提交于 2020-01-16 03:55:07
问题 This is a test senario, made with temporary tables to illustrate the problem. Pretend table @userdata has index on userid and table @users has index on id Why is the first select unable to use index, I assumed it would perform better in 1 subselect than in 2 subselects? Version - Microsoft SQL Server 2008 R2 (RTM) Compatibility level - SQL Server 2000. -- test tables DECLARE @userdata TABLE(info VARCHAR(50), userid INT) DECLARE @users TABLE(id INT, username VARCHAR(20), superuser BIT) -- test

Output dates in MMDDYYYY format efficiently

妖精的绣舞 提交于 2020-01-16 01:24:39
问题 I have the below script which results YYYYMMDD . However, I need the result in MMDDYYYY format. Can someone please assist? I want no dash/stroke/periods between the numbers. SELECT CONVERT(VARCHAR(10), RowUpdateDateTime, 112) from MyTable Results: 20141113 I want it to look like 11132014 . I tried the FORMAT syntax but it was taking forever to run. 回答1: Take a look at the CONVERT() documentation: none of formats match exactly what you're looking for. It looks like 110 is the closest. We can