sql server - Conditional Common Table Expression (CTE) in SQL -


After

I am trying to select a product category tree hierarchy in SQL.

My code looks as follows. When I'm trying to get a dynamic sort order using the IF or Case on the sort order parameter.

The row commented on @SortOrder 'sortorder' should be active. I have tried to add a statement around it, but I have failed ...

< P> Can you help?

  create process [dbo]. [ProductCategory_SelectHierarchy] @SortOrder varchar (30) Set Naknet current; As categories (number, ParentId, SortOrder, RowOrder) (select parentCategory.Id, parentCategory.ParentId, parentCategory.SortOrder, --cast (REPLACE (STR (parentCategory.SortOrder, 8), ',' 0 ') varchar (2)) as the varchar (4) cast (DatePart (year, parentCategory.DateCreated)) + Cast 'RowOrder' cast (DatePart (MONTH, parentCategory.DateCreated) as varchar (30) + Cast (DatePart (DD, parentCategoryDateCreated) varchar (2)) + Cast (DatePart (hour, parentCategory.DateCreated) varchar (2)) varchar (50)) ProductCategories from parentCategory as 'RowOrder' as where ParentId = 0 Select all UNION as childCategories.Id, as childcategories. ParentId, childCategories.SortOrder, --cast (categoriesRoder + Change (STR (childC ategories.SortOrder, 8), '', '0 RowOrder') varchar (30)) as' cast (Categories.RowOrder + Cast 'DatePart (year, childCategories.DateCreated) (Date, Date, childCategories.DateCreated) DatePart (DD, childCategories.) As the varchar (4)) + Cast (varchar (2) Varchar (50)) as the varchar (2)) + cast (DatePart (hour, childCategories.DateCreated) as varchar (2)) as ProductCategories childCategories.ParentId = Categories.Id Categories on ChildCategories Select Join PC from 'RowOrder'. * Join categories from categories.RowOrder categories ProductCategories pc.Id on PC = Categories.Id command RowOrder    

You should be able to sort it properly:

  depending on case when @SortOrder = 'date_column' then the conversion (VARCHAR (20), date_column, 120 ) WHEN @SortOrder = 'CUSTOMER_ID' then correct (Repeat ('0', 20) + Cast (CUSTOMER_ID as VARCHAR (20)), 20) When @SortOrder = 'name' then name end SORT_ORDER  

To terminate the same data type as your so Getting all of the rtable columns (or expressions)


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -