HTML title attribute line break - html

I am trying to generate a tool tip text using SQL. The text generated is passed as Title Attribute in HTML. There needs to be some newline characters generated in the tool tip.
I have used the following -
CHAR(13); CHAR(10); <br>; \n.
However in all cases, I see the character itself in HTML and not a new line.
Any idea how to achieve this?
the SQL is something like this
(SELECT
STUFF(';' + WOR.OrderNo + ' - ' + P.ProductNo + ' - ' + CAST(CAST(ROUND(WOR.OrderQuantity , 0) as int) as varchar(20)) + '; <br/> ', 1, 1, '')
FROM
[ORDER] WOR
JOIN
PRODUCT P
ON P.ID = WOR.ProductID
JOIN
PRODUCT_GROUP PGR
ON P.ID = PGR.ProductID FOR XML PATH(''),TYPE).value('.','nvarchar(MAX)')```
And the Tootip that I see is the following
```SMU_100000021 - A-WHEL-001 - 100;<br/>SMU_100000023 - A-WHEL-001 - 90;<br/>```

The CHAR(10) did the trick.
(SELECT
STUFF(';' + WOR.OrderNo + ' - ' + P.ProductNo + ' - ' + CAST(CAST(ROUND(WOR.OrderQuantity , 0) as int) as varchar(20)) +' '+ CHAR(10) + ' ', 1, 1, '')
FROM
[ORDER] WOR
JOIN
PRODUCT P
ON P.ID = WOR.ProductID
JOIN
PRODUCT_GROUP PGR
ON P.ID = PGR.ProductID FOR XML PATH(''),TYPE).value('.','nvarchar(MAX)')

Related

SQL Server: convert html tags

I decided to ask it here after failing to find answer on my question. I think the solution is easy.
I have a SQL query like this:
SELECT
e.*,
STUFF((SELECT ',' + '<a href=test.aspx?tab=2&name=wil&item_id=[er.id]>' + er.name + '</a>'
FROM dbo.role er
FOR XML PATH ('')), 1, 1, '') all
FROM
dbo.worker e
The only problem is that the output is of the html tags are weird like
< = <
& = &
> = >
How can I make it normal sign's ?
You can use type directive to avoid xml encoding
SELECT e.*,
Stuff((SELECT ','
+ '<a href=test.aspx?tab=2&name=wil&item_id=[er.id]>'
+ er.NAME + '</a>'
FROM dbo.role er
FOR XML PATH (''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM dbo.worker e

SQL Server : add a character in a string between 2 comma-separated values

declare #qry nvarchar(max)
set #qry='IType, INum, IDate, PO, FCode, Tx, Fr, TI, Not'
select #qry = 'select distinct ti.ID,' + #qry +
' from tblInfo ti inner join tblheadr th on ti.IA=1 AND ti.BId = ' +
CAST(#BId as varchar) + ' AND th.CUId =' + CAST(#UserID as varchar)
Now I want my query as
select distinct
ti.ID, ti.IType, ti.INum, ti.IDate, ti.PO, ti.FCode, ti.Tx, ti.Fr, ti.TI, ti.Not
from
tblInfo ti
inner join
tblheadr th on ti.IA = 1 AND ti.BId = 285 and th.CUId = 2
I need to add 'ti.' for each value in #qry..
Can you suggest me how to separate it add ti. in between #qry ?
Here's a shorter solution:
SET #qry = 'IType, INum, IDate, PO, FCode, Tx, Fr, TI, [Not]'
SET #qry = 'ti.' + REPLACE(#qry, ', ', ', ti.')
You should put Not in square brackets cause it's a reserved SQL keyword.
This should work:
select #qry =
'select distinct ti.ID,'
+ replace(replace(replace(#qry, ', Not', ', [Not]'), #qry, 'ti.' + #qry), ', ',', ti.')
+' from tblInfo ti inner join tblheadr th on ti.IA=1 AND ti.BId = '
+ QUOTENAME(CAST(#BId as varchar(max)), '''')
+ ' AND th.CUId ='
+ QUOTENAME(CAST(#UserID as varchar(max)),'''')
You really should specify size for the varchar casts, and also note that notis a keyword that needs to be escaped, so I added that. Most likely you also need to use QUOTENAME to enclose the parameter values in quotes (I added that too).
The resulting#qryvariable will be this:
select distinct
ti.ID,IType, ti.INum, ti.IDate, ti.PO, ti.FCode, ti.Tx, ti.Fr, ti.TI, ti.[Not]
from tblInfo ti
inner join tblheadr th on ti.IA=1 AND ti.BId = 'BidA' AND th.CUId ='UserA'
If the variables are declared as:
declare #userid nvarchar(max) = 'UserA'
declare #bid nvarchar(max) = 'BidA'
On a side note: looking at your join between tblInfo and tblHeadr it seems like there's no join condition, only conditions on the separate tables (that should probably be written in a WHERE clause). Maybe you forgot the join th.? = ti.? ?

Computed Column with relationships

I have a table, MapLocation, which has a column and two relationships with tables that have a field that really need to be displayed as a single concatenated value. I was thinking this was a perfect case for a computed column, but not sure how to go about it.
MapLocation MaoNo Section
_____________________ _____________________ _____________________
MapNoId MapNoId SectionId
SectionId MapNumber (int) Section (int)
Identifier (nvarchar)
LocationName (nvarchar)
LocationName = "MapNUmber - SectionNumber - Identifier"
ex: 20 - 03 - SW4
How would I write that? I haven't done much with computed columns or concatenating in SQL.
Edit:
I need an actual computed column that is automatically updated, im looking for the formula. Or is this more of a function/trigger? Its possible, I certainly barely know what I'm doing. The idea is that I dont want to have to do two more server calls and concatenate these values client side.
You would use something like this to get the value:
select cast(n.MapNumber as nvarchar(10)) + ' - ' -- cast the MapNumber
+ cast(s.SectionId as nvarchar(10)) + ' - ' -- cast the SectionId
+ l.Identifier
from MapLocation l
left join MaoNo n
on l.MapNoId = n.MapNoId
left join Section s
on l.SectionId = s.SectionId
Then if you need to perform an UPDATE:
update l
set l.LocationName = (cast(n.MapNumber as nvarchar(10)) + ' - '
+ cast(s.SectionId as nvarchar(10)) + ' - '
+ l.Identifier)
from MapLocation l
left join MaoNo n
on l.MapNoId = n.MapNoId
left join Section s
on l.SectionId = s.SectionId
Edit #1 - you can use a TRIGGER:
CREATE TRIGGER trig_LocationName
ON MapLocation
AFTER INSERT
AS
Begin
update MapLocation
set LocationName = (cast(n.MapNumber as nvarchar(10)) + ' - '
+ cast(s.SectionId as nvarchar(10)) + ' - '
+ i.Identifier)
from Inserted i
left join MaoNo n
on i.MapNoId = n.MapNoId
left join Section s
on i.SectionId = s.SectionId
where MapLocation.MapNoId = i.MapNoId -- fields here to make sure you update the correct record
End

Script table schema using tsql

I am writing code to script CREATE TABLE statements for all tables across all databases on a server.The code below will work for a specific database. Would like some ideas on how to modify it, so it can script table schema for all the tables.
select 'create table [' + so.name + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' END
from sysobjects so
cross apply
(SELECT
' ['+column_name+'] ' +
data_type + case data_type
when 'sql_variant' then ''
when 'text' then ''
when 'decimal' then '(' + cast(numeric_precision_radix as varchar) + ', ' + cast(numeric_scale as varchar) + ')'
else coalesce('('+case when character_maximum_length = -1 then 'MAX' else cast(character_maximum_length as varchar) end +')','') end + ' ' +
case when exists (
select id from syscolumns
where object_name(id)=so.name
and name=column_name
and columnproperty(id,name,'IsIdentity') = 1
) then
'IDENTITY(' +
cast(ident_seed(so.name) as varchar) + ',' +
cast(ident_incr(so.name) as varchar) + ')'
else ''
end + ' ' +
(case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' +
case when information_schema.columns.COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ information_schema.columns.COLUMN_DEFAULT ELSE '' END + ', '
from information_schema.columns where table_name = so.name
order by ordinal_position
FOR XML PATH('')) o (list)
left join
information_schema.table_constraints tc
on tc.Table_name = so.Name
AND tc.Constraint_Type = 'PRIMARY KEY'
cross apply
(select '[' + Column_Name + '], '
FROM information_schema.key_column_usage kcu
WHERE kcu.Constraint_Name = tc.Constraint_Name
ORDER BY
ORDINAL_POSITION
FOR XML PATH('')) j (list)
where xtype = 'U'
AND name NOT IN ('dtproperties')
Since you seem determined on TSQL, a quick and dirty solution would be to use the (undocumented) sp_foreachdb stored procedure. That will give you the name of every database on the sql instance, so you would just USE each database in turn and run TSQL that you posted (after escaping all the single qoutes in it).
Heres a shortened example:
EXECUTE sp_msforeachdb 'USE [?]
select ''create table ['' + so.name + ''] ''
from sysobjects so
-- the rest of your escaped TSQL goes here...
-- then finish with a closing quote:
'

Why is my CAST returning null/nothing?

When I attempt to cast my FLOATS into CHARS in this procedure, I get null values in the database. Location is a Geospatial field. What am I doing wrong?
CREATE DEFINER=`me`#`%` PROCEDURE `UpdateLocationByObjectId`(IN objectId INT,
IN latitude FLOAT,
IN longitude FLOAT)
BEGIN
UPDATE Positions P
JOIN Objects O ON P.Id = O.PositionId
SET P.Location = GeomFromText('Point(' + CAST(latitude AS CHAR(10)) + ' ' + CAST(longitude AS CHAR(10)) +')')
WHERE O.ObjectId = objectId;
END
If I use this as a test, it works fine.
CREATE DEFINER=`me`#`%` PROCEDURE `UpdateLocationByObjectId`(IN objectId INT,
IN latitude FLOAT,
IN longitude FLOAT)
BEGIN
UPDATE Positions P
JOIN Objects O ON P.Id = O.PositionId
SET P.Location = GeomFromText('Point(10 10')')
WHERE O.ObjectId = objectId;
END
Change this line
SET P.Location = GeomFromText('Point(' + CAST(latitude AS CHAR(10)) + ' '
+ CAST(longitude AS CHAR(10)) +')')
To
SET P.Location = GeomFromText(concat('Point(' , CAST(latitude AS CHAR(10)) , ' '
, CAST(longitude AS CHAR(10)) ,')'))
The + operator is adding your text values ('10' + '10') = 20
So the center part evaluates to 'Point(' + 20 + ')', adding text that cannot be read as number + numbers evaluates to NULL.
Only the concat function can concatenate strings.
In fact this code will work just as well:
SET P.Location = GeomFromText(concat('Point(', latitude, ' ', longitude,')'))