If you ever google sql for loop, just stop right there.
SQL is by definition a for loop.
A SELECT statement does nothing more than iterate over the rows in the database that satisfy the conditions of the query.
Therefore, if you want a for loop that sets a variable and then does some operation on one row at a time, JUST STOP. What you really want is a join somehow, and you can get it all done much simpler in terms of code and processing power. It will be easier to debug and to understand, and you won't have to worry about setting up the attendant variables to process a true for loop. SQL is a for loop, use it that way!
Popular Posts
-
Excel likes to write numbers into scientific notation. This raises a problem if you are trying to message data into a database from an exce...
-
View the complete solution at https://github.com/adamvaul/React-Server-Side-Web-Forms . I love ReactJS.Net. It is a framework for manag...
Saturday, November 7, 2015
Friday, June 12, 2015
Rules of Coding
Rules of Coding
- The most important language you will ever code in: English (or the common spoken language used in your company). -- So that everyone understands what you wrote and why.
- Never use a regular expression without saying in plain English what it is doing. Regex is so hard to understand!!! Example: what the heck is this doing. Found in the gruntfile.js of bootstrap at https://github.com/twbs/bootstrap/blob/master/Gruntfile.js return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
- If you are going to modify something extensively, you are better off writing a new one and swapping it in when it is ready to go live. For example, if you are extensivley modifying an email template that is currently being used in production, just make a new template with a creative name and use that, so you don't possibly mess up code that is still using the old template. --This way you can also keep a clean backup of the old template and be able to rollback quickly.
- No one cares. No matter how much you think a given coding principle is important, there is nothing worse than alienating your fellow co-workers by sticking to a principle. It may still be important, but no one cares in the end. Try living without it for a day and you'll find you are still alive.
- Don't bother committing code if you haven't tested it. There are runtime errors and compile time errors. So even if your code builds, it doesn't mean it's even going to work. This will save you time in the end, believe me. You will end up having to change your code that doesn't work, or rollback a feature that you comitted and later find out will never work (e.g. I added logic to update the CreatedDate of some table in the database, and come to find out you can't update a database autopopulated field. So I get to rollback the "addition" I made across 5 files).
Friday, June 5, 2015
Using XML to pass dynamic paramters to SQL stored procedure.
You can use an XML string to pass any number of options to a SQL stored procedure like so
Use a scalar valued function to get one value at a time, which is still very fast unless you have hundreds of key-value pairs with XML likeMyValue
Use like this
Use a scalar valued function to get one value at a time, which is still very fast unless you have hundreds of key-value pairs with XML like
Use like this
USE [SaveOn]
GO
/****** Object: StoredProcedure [dbo].[sp_GetClubUserRenewals] Script Date: 5/6/2015 12:37:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Gets count of users and total dollar value from user renewals per club.
-- =============================================
ALTER PROCEDURE [dbo].[sp_GetClubUserRenewals]
@Filters NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
--Declare variables
DECLARE @BeginDate DATETIME
--Parse out values from xml object
SELECT @BeginDate = dbo.GetFilterValue(@Filters,'BeginDate');
--Select statement
SELECT * FROM SOMETABLE WHERE(@BeginDate IS NULL OR DateField >= @BeginDate)
END
And here is the scalar valued function.
GO
/****** Object: UserDefinedFunction [dbo].[GetFilterValue] Script Date: 5/6/2015 12:35:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Gets the value of a single node in a Filters xml object defined like Value
-- =============================================
ALTER FUNCTION [dbo].[GetFilterValue](
@Filters XML,
@FilterName NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
-- Declare the return variable here
DECLARE @RetVal NVARCHAR(MAX);
-- Add the T-SQL statements to compute the return value here
SELECT @RetVal = NULLIF(LTRIM(RTRIM(node.value('.','nvarchar(max)'))),'')
FROM @Filters.nodes('/Filters/*') AS Nodes(node)
WHERE node.value('local-name(.)','nvarchar(max)') = @FilterName;
-- Return the result of the function
RETURN @RetVal;
END;
Or get them all at once with a table valued function:
USE [SaveOn]
GO
/****** Object: UserDefinedFunction [dbo].[ParseFilters] Script Date: 5/4/2015 3:18:07 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Takes xml like MyValue and returns all name-value pairs in a table, removing empty entries.
-- =============================================
ALTER FUNCTION [dbo].[ParseFilters](
@XML XML)
RETURNS @RtnTable TABLE(ID INT IDENTITY(1,1),
Name NVARCHAR(MAX),
Value NVARCHAR(MAX))
AS
BEGIN
INSERT INTO @RtnTable(Name,
[Value])
SELECT node.value('local-name(.)','nvarchar(max)') AS Name,
NULLIF(LTRIM(RTRIM(node.value('.','nvarchar(max)'))),'') AS Value
FROM @XML.nodes('/Filters/*') AS Nodes(node);
RETURN;
END;
Tuesday, April 28, 2015
ReactJS.Net in Web Forms App with Server-side rendering
View the complete solution at https://github.com/adamvaul/React-Server-Side-Web-Forms.
I love ReactJS.Net. It is a framework for managing a React driven website in Visual Studio. Recently, I got interested in React and wanted to see if I could make it work in a web forms application (not an MVC application like all the demos show). I got a very simple demo to work based on the Chat tutorial on their website.
In this web forms app there is a little bit of code to render the html string to the page, and then there is a small snippet to add the block to register the react compenents client side. All of this magic happens with these few lines of code.
I followed the tutorials here at the ReactJS website http://reactjs.net/getting-started/tutorial.html. I also found this article to be helpful but I had to figure out the last part with env.GetInitJavaScript(); on my own. The article is here at http://xabikos.com/server%20side%20templating/web%20development/2015/03/18/using-reactjs.net-in-web-forms.html
And
In the hmtl:
asp:Literal ID="app" runat="server"
asp:Literal ID="litInitJS" runat="server"
And then in the code behind:
var env = AssemblyRegistration.Container.Resolve
var homeController = new HomeController();
var data = homeController.Get();
var commentBoxOptions = new
{
initialData = data,
url = "api/Home",
submitUrl="api/Home/addcomment",
pollInterval=2000
};
var commentBoxComponent = env.CreateComponent("CommentBox", commentBoxOptions);
app.Text = commentBoxComponent.RenderHtml();
string initJS = env.GetInitJavaScript();
litInitJS.Text = initJS;
I did not implement all the features like they did, but the application works.
View it at https://github.com/adamvaul/React-Server-Side-Web-Forms.
Subscribe to:
Posts (Atom)