Concatenate row values T-SQL

Have a look at this DECLARE @Reviews TABLE( ReviewID INT, ReviewDate DATETIME ) DECLARE @Reviewers TABLE( ReviewerID INT, ReviewID INT, UserID INT ) DECLARE @Users TABLE( UserID INT, FName VARCHAR(50), LName VARCHAR(50) ) INSERT INTO @Reviews SELECT 1, ’12 Jan 2009′ INSERT INTO @Reviews SELECT 2, ’25 Jan 2009′ INSERT INTO @Users SELECT 1, ‘Bob’, … Read more

Create HTML Table with SQL FOR XML

select (select p.ProblemType as ‘td’ for xml path(”), type), (select p.Onset as ‘td’ for xml path(”), type), (select p.DiagnosisStatus as ‘td’ for xml path(”), type) from tblProblemList p where p.PatientUnitNumber = @PatientUnitNumber for xml path(‘tr’) To add the header as well you can use union all. select (select ‘Problem’ as th for xml path(”), type), … Read more

T-SQL: SUSER_SNAME vs SUSER_NAME?

If you call the function without an argument they will both return the same value. But they do take different arguments: SUSER_SNAME() takes the varbinary(85) SID of a login as argument SUSER_NAME() takes the integer principal_id of a login You can verify this like: select suser_name(principal_id) , suser_name(sid) , suser_sname(principal_id) , suser_sname(sid) from sys.server_principals where … Read more

Declare a variable in DB2 SQL

I assume this forum posting, which I quote fully below, should answer the question. Inside a procedure, function, or trigger definition, or in a dynamic SQL statement (embedded in a host program): BEGIN ATOMIC DECLARE example VARCHAR(15) ; SET example=”welcome” ; SELECT * FROM tablename WHERE column1 = example ; END or (in any environment): … Read more

SQL update query syntax with inner join

The SET needs to come before the FROM\JOIN\WHERE portion of the query. UPDATE CE SET sJobNumber = AD.JobNumber FROM CostEntry CE INNER JOIN ActiveCostDetails As AD ON CE.lUniqueID = AD.UniqueID WHERE CE.SEmployeeCode=”002″ AND SubString(CostCentre, 1, 1) = sDepartmentCode AND substring(CostCentre, 3, 1) = sCategoryCode AND substring(CostCentre, 5, 2) = sOperationCode