Click here to Skip to main content
14,971,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there

I'm struggeling by creating a regex to parse the column names whitin a given t-sql statement.
This is my test sql statement to work with:

SQL
SELECT
	dbo.Reservation.Id,
	dbo.Reservation.Beginn,
	dbo.Reservation.Ende,
	RessourceBezeichnung = (SELECT Value FROM dbo.Text WHERE FieldOid = dbo.Ressource.BezeichnungOid AND LanguageId = 'de'),
	(SELECT Value FROM dbo.Text WHERE FieldOid = dbo.Ressource.BezeichnungOid AND LanguageId = 'de') AS StatusBezeichnung,
	HasSubreservationRaum = ISNULL(CASE 
			WHEN (
					Reservation.ReservationId IS NULL
					AND EXISTS (
						SELECT NULL
						FROM dbo.Reservation AS ChildReservation WITH (NOLOCK)
						WHERE ChildReservation.ReservationId = dbo.Reservation.Id
							AND ChildReservation.RessourcetypId = 1
						)
					)
				OR (
					Reservation.ReservationId IS NOT NULL
					AND EXISTS (
						SELECT NULL
						FROM dbo.Reservation AS ChildReservation WITH (NOLOCK)
						WHERE ChildReservation.ReservationId = dbo.Reservation.ReservationId
							AND ChildReservation.RessourcetypId = 1
						)
					)
				THEN CAST(1 AS BIT)
			ELSE CAST(0 AS BIT)
			END, CAST(0 AS BIT))
FROM dbo.Reservation WITH (NOLOCK)


There are three ways a column can be name
- Just select the column name (e.g. dbo.Reservation.Id --> Id)
- Define the column name followed with an equal (e.g. HasSubreservationRaum = (A subquery)
- Or alias is it with the as (e.g (SELECT Value FROM dbo.Text WHERE FieldOid = dbo.Ressource.BezeichnungOid AND LanguageId = 'de') AS StatusBezeichnung)

Any ideas how to solve this?

Regards,
Lukas
Posted
Comments
Kornfeld Eliyahu Peter 6-May-14 14:23pm
   
Are you want to parse an SQL query (that you have as a string) for specific column names?

1 solution

Why to force doors wide open?

TSQLParser[^] class will do it for you.
SQL Parser[^]
Parse Transact SQL to Check Syntax[^]

If it wont be enough, let me know and i'll improve my answer ;)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900