0

Is it possible to add a JavaScript variable in an SQL Server database database (run an SQL query or stored procedure)? If so, how?

4
  • 1
    not sure what you mean - I would think you can insert whatever value you can output - can you explain with a little more detail? Commented May 3, 2011 at 4:56
  • i want save value of some javascript variable within a function in sql server database. like save last position of one image that changed by user. Commented May 3, 2011 at 4:59
  • I see this question almost everyday now on SO. Commented May 3, 2011 at 5:02
  • Is this question restricted to Microsoft SQL Server or is it for any SQL database? Commented Nov 21, 2012 at 17:30

5 Answers 5

4

With JavaScript only, no you cannot (unless you're using Node.js of course).

The point is, you'll need some server-side code to interact with your database. You can use JavaScript in the browser to make an Ajax call to a server-side script (PHP, Ruby, Python, ASP.NET, Node.js, etc.) that performs the interaction with the database.

Sign up to request clarification or add additional context in comments.

1 Comment

This would be your most ideal solution. Node.js is pretty new and it would make sense to get your hands dirty on it and learn it. Otherwise, you can use Ajax to send Client Side calls to a server script and your server script can save or update the data in your database.
1

You can not access the database from JavaScript. You need to send your data to PHP, Ruby on Rails or ASP.NET or whatever you are using to implement the back end.

2 Comments

no, AJAX will invoke your server and the server will do all the stuff. this means you are sending your javascript data to server using ajax
yes, agree AJAX is javascript... which make a http request.. but it not invoke any database without your server side implementation. The invoking db is mainly done by the server side technology.
1

You can use Ajax to post to a web service of some sort (or any other server side script) to pass the JavaScript value. The web service would access the database and save it...

Comments

0

Set value of variable in the hidden field and get value of that field at server side when submit form and save it into the database....

1 Comment

With a JS set of a hidden field, the server won't access it without a repost, and it won't save without a repost - which I believe in this case defeats the purpose...
0

You can easily access a database from JavaScript in Node.js.

If you're doing it from the browser, you'll most definitely need some kind of mid-tier service which translates HTTP queries to the relevant database driver (you can write that in any language you want - PHP, JavaScript, ASP.NET, etc.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.